1

I have a site running with ASP.NET MVC 1 and have encountered a bug where a bool in my model is being reset to default because the value is not used in the view.

The code for the Controller:

public ActionResult Edit(Guid id)
{
    Service service = GetService(id);

    if (service == null)
        return View("NotFound");

    return View(service);
}

when the view code had:

<label for="IsActive"> Is Active: </label> 
<%= Html.TextBox("IsActive") %>

it was working fine however once it was deleted the isactive field was always returned as false. I no longer want the isactive to be seen or modified from this view but removing it has caused the value to be lost, i have tried setting the value and not displaying it with

<% Html.TextBox("IsActive"); %>

but that still had it defaulting the value to false

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(Service service)
{
    //UpdateLogic
}

in the post method, regardless what IsActive had been before, the Service.IsActive is always false

basically i was just wondering how i could get the site to keep the value it was passed without displaying the text box in the view (have tried googled but failed to get the right combination of words for a decent result)

4 Answers 4

3

Can you add it as a Hidden on the page:

<%= Html.Hidden("IsActive"); %>
Sign up to request clarification or add additional context in comments.

3 Comments

thanks thats perfect (it needs the equals to work but other than that its great) - <%= Html.Hidden("IsActive") %>
this is not a good way to fix this. it will make it very easy to edit the value in the browser anyway, check my fix.
@Stefanvds - I don't like your fix. It seems like you are putting a lot of logic in the controller that shouldn't be in the controller. I prefer the ViewModel approach over yours.
3

You can hide it as a Hidden field on the page:

<% Html.Hidden("IsActive"); %>

BUT!! it is not recommended to do that. since it is very easy to manipulate the value and you dont want that.

the best way is just to get the value from the DB.

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(Service service)
{
    Service originalService = GetService(id);

    UpdateModel(originalService, new string[] { "Field1thatyouwanttoupdate", "fieldname2" });
}

this way you update all fields that you want, but NOT the IsActive field.

8 Comments

This should be accepted as the answer. Only bind the fields that you actually allow the user to update. Never trust user input.
@Jace Rhea - If you should only bind the fields that you actually allow the user to update, then this is not the answer - Yakimych's answer about ViewModels is.
@Jace Rhea - Well then why even send the IsActive to the client? Send a ViewModel, get it back, convert it to your Data Model, and let your ORM handle the updates, not you. (Sorry, not trying to be a jerk, just trying to learn)
@Jace Rhea - But you are returning a Service object ... which has a property of IsActive ... which is getting set to something and being sent to the client and back to the server. Seems inefficient. (Sorry Stefanvds, we are hijacking your comments)
I do like this approach and will probably use it in the future, however at the moment I have been put on a project which was started by someone else and I have been told to get it working as quickly as possible and so am trying to rewrite as little as possible. because of this the best answer, for my purposes in this project, was martins which is why I voted it as the correct answer.
|
2

As a quick solution you can use a hidden field instead of the textbox and store the value there.

In general, though, if you don't want to display or update the value, why send it to the view? Create a ViewModel with only the data you actually need in the view and post it back and forth.

1 Comment

I like this approach, a lot of work, but I try to create ViewModels whenever I can.
-1

Create it as a hidden value:

<input id="IsActive" type="hidden" value="true" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.