tags:

views:

56

answers:

4

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)

+3  A: 

Can you add it as a Hidden on the page:

<%= Html.Hidden("IsActive"); %>
Martin
thanks thats perfect (it needs the equals to work but other than that its great) - <%= Html.Hidden("IsActive") %>
Manatherin
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
@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.
Martin
+2  A: 

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.

Yakimych
I like this approach, a lot of work, but I try to create ViewModels whenever I can.
Martin
A: 

Create it as a hidden value:

<input id="IsActive" type="hidden" value="true" />
Dismissile
+3  A: 

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.

Stefanvds
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
@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.
Martin
@Martin Sorry, but I don't agree. By explicitly saying which fields to bind in the UpdateModel method (leaving out IsActive) the IsActive field will never be updated. Yes, you can create a separate viewmodel class as well but this seems like extra, unnecessary work.
Jace Rhea
@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)
Martin
@Martin I never said you should said the IsActive field to the client, so I'm not sure where you are reading that.
Jace Rhea
@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)
Martin
If you want to email me about this feel free. But you are reading things into my comment that I am not saying. You are not sending the IsActive flag to the client. Just because the method takes a Service object does not mean that all properties of the service object are sent to the client. Inefficient? As opposed to creating a separate class that is going to require separate maintenance whenever the Service class changes? There are many ways to solve a problem. I believe this is the more correct, more maintainable approach. I understand you disagree. Let's leave it at that.
Jace Rhea
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.
Manatherin