MVC3 DropDownListFor Model is null on PostBack - asp.net-mvc-3

I am using a DropDownListFor like this:
#Html.DropDownListFor(model => model.SelectedOrganisationValue, new SelectList(Model.OrganisationList, "Value", "Text"))
And I am also using:
[Required]
As Attribute in the View.
So when I PostBack the View to the Server and the Required Attribute will fail, the View is showed again, but then Model is null. So I will get a NullReferenceException in Model.OrganisationList.

That is the default behaviour. As you know ,MVC does not rely on ViewState, It can not keep the content inside the drop down across Postbacks (generic term). You need to repopulate it again in the ActionMethod.

Related

Using model data in a form in a partial view

I have the following code in a partial view:
#model MyOrganization.MyApp.Models.ProductListing
#using (Ajax.BeginForm("TagProduct", new AjaxOptions() { UpdateTargetId = "FormContainer" , OnSuccess = "$.validator.unobtrusive.parse('form');" }))
{
<p>
#Html.LabelFor(m => m.ModelNumber):
#Html.EditorFor(m => m.ModelNumber)
Tag Product with This Model Number
#(Html.ValidationMessageFor(m => m.ModelNumber))
</p>
}
The viewmodel that this partial view gets is instantiated and many of its properties are hydrated by the view that contains this partial view. However, when the submit is called here, the viewmodel that the controller gets has only the ModelNumber property hydrated. All the other properties are null as if a new instance is created by the partial view with only the property that was edited (ModelNumber) getting a value.
I know that the viewmodel instance passed to the partial view has all the other property values, because if I add an #Html.EditorFor(m => m.SerialNumber) I can see the SerialNumber value that the containing view had rendered in the browser's textbox and I also get it back in the controller when the form is submitted. However, I don't want an editor for the SerialNumber property on the form - I just want it back in the controller when it is submitted.
How can I can I get the entire model back to the controller as it was passed in to the partial view with only the changes that the partial view made?
When it posts the form, it DOES create a new copy of the model with whatever values you posted to it. If you need the other values, put #Html.HiddenFor the other elements you need posted.

Binding current value to SelectList's display

I've been able to correctly display a dictionary as a dropdownlist as well as pull it's value on a page submit. But on the GET (initial display) the selected item does not reflect the object's value. My controller is passing state:
ViewData["Status"] = new SelectList(AppHelper.WebinarStatuses, "Key", "Value", selectedStatus);
the View:
<%: Html.DropDownListFor(m => m.Status, (ViewData["Status"] as SelectList), Model.Status )%>
I understand that I need to find the right overload of the DropDownListFor helper - I don't understand how I go about doing that. Small picture, what syntax forces the select list to display what the controller's sending - bigger picture, how do I discover/interpret which overload does what?
profuse thanks

ModelState validation errors for properties that are not present in my view

Suppose this view :
#Html.HiddenFor(model => model.Batiment.Client.Id)
#Html.LabelFor(model => model.Batiment.Code)</td>
#Html.EditorFor(model => model.Batiment.Code)</td>
<br>
#Html.LabelFor(model => model.Batiment.Nom)</td>
#Html.EditorFor(model => model.Batiment.Nom)</td>
When I submit my form on the controler the ModelState is invalid for the property "Nom" required into my class Client. Is true, the metadata in my class Client is set to required but I dont include this field into my view...! Why Mvc raise this error?
Can I hide a field (like Id) without specify all the required field into my view?
Errors enter model state during binding, therefore you could exclude your property from binding by including the following in your action method signature:
public ActionResult Register([Bind(Exclude="PropertyName")] UserViewModel user)
{
// Your logic here
}
This should exclude PropertyName from binding, therefore error won't enter your model state and your validation should succeed. Just to add, I think that this is more of a hack, rathern then solution. If you need only a part of your view model, than this view model should not be used and you should really consider creating a new view model without this property.
It might look very similar and it might seem like duplicate code, but it isn't. It promotes seperation of concerns and you should see the benefit of doing this in the near future when it comes to extending/modifying your application.
No, the validation is done on the model, not the view, why it is called 'model state'
You need to create another view for this scenario. This is exactly what ViewModels are for.
Then you can use a tool like AutoMapper to easily to copy the properties between this view model to your base model.

Where does asp.net mvc text box input value is taken?

I have a url like this:
http://localhost/products/AddFeature?code=SMW
in the controller action, I create a model and set the
model.Code = "123112".
The View has this
#Html.EditorFor(model => model.Code)
the final rendered html always shows "SMW" as the value.
Is this a bug where the query string value overrides the assigned value ?
Does anyone got same results or I need some coffee ?
Clear the model state before returning your view. Be sure that you haven't missed some model error before you do that. The helpers use the ModelState, then the models for their values.
ModelState.Clear();

mvc 3 html attributes

i have been playing around with MVC. I am currently stumped on with html helper methods. One thing i have noticed is that I cant really cant apply the ASP.NET Web Form logic into MVC. To explain further, in ASP.NET I could create a Label control and assign it some text data and then read the text data.
However, in MVC, I cant seem to do the same with #Html.LabelFor/#Html.Label, I have realised that once you do a POST from your form, the value from the Label is not bound back into my view model. However, if I use an EditorFor or TextBoxFor, I can get values bound to my viewmodel upon POST.
My question what html hlper method should I use to display text as readonly but yet be able to bind back to my viewmodel upon post ? I have tried TextBoxFor with its html attributes set to disabled and readonly but no luck.
Appreciate any pointers.
thanks
You should be able to bind the readonly attribute to the TextBox by passing in htmlAttributes as the 2nd parameter of the TextBoxFor method:
<%=Html.TextBoxFor(m => m.SomeProperty, new { #readonly = "readonly" }) %>
On MSDN: InputExtensions.TextBoxFor Method (HtmlHelper, Expression>, Object)
If you're trying to maintain the Label value you can use a combination of the LabelFor and HiddenFor methods.
I don't know why you would need to do this though, since you should be able to get the DisplayText attribute or the Property Name from the property.
<%=Html.LabelFor(m => m.SomeProperty) %>
<%=Html.HiddenFor(m => m.SomeProperty) %>
but this doesn't make a lot of sense since the usual syntax would be:
<%=Html.LabelFor(m => m.SomeProperty) %>
<%=Html.TextBoxFor(m => m.SomeProperty) %>
Note that if you use the disabled attribute the input will not be posted when the form is submitted
This is expected behaviour, only values form elements are added to your Model on POST so your label will be ignored. To get around this duplicate your label value in a hidden field
Html.HiddenFor(model => model.FieldName)
or
Html.Hidden("FieldName", model.FieldName)

Resources