Model Public Class CategoriesEditViewModel Public Property Categories As List(Of cihCategoryOrgDef) Public Property Category As cihCategoryOrgDef Public Property lists As cihLists = New cihLists() Public Property SelectedItem As String Public Sub New() End Sub Public Sub New(codId As Guid) SelectedItem = codId.ToString lists.loadCategoryOrgDef(ZenCommon.CurrentOrgId) lists.loadClubWaivers(ZenCommon.CurrentOrgId) Categories = lists.organizationClubCategories Category = (From x In Categories Where x.codId = codId Select x).SingleOrDefault() End Sub End Class Controller <HttpGet> Function Edit(codId As Guid) As ActionResult Dim model As CategoriesEditViewModel = New CategoriesEditViewModel(codId) Return View(model) End Function <HttpPost> Function Edit(category As CategoriesEditViewModel) As ActionResult If ModelState.IsValid Then 'make change to DB Return RedirectToRoute("club_category_list") End If Return View(category) End Function Edit Page View @ModelType CheckImHereMVC.CategoriesEditViewModel @Using Html.BeginForm() @<fieldset> @Html.EditorFor(Function(m) m.Category.codDescr) @Html.DropDownListFor(Function(m) m.SelectedItem, New SelectList(Model.lists.organizationClubWaivers, "waiverId", "waiverName", Model.lists.organizationClubWaivers)) <input type="submit" value="Save" /> </fieldset> The HTTPGet Edit page works fine. It goes to a url such as categories/edit/1 and gives me the correct information corresponding to that particular Category However, when i change some data, and click the Save button. i am redirected to the HTTPPost Edit function, however my ‘category` (value being passed in) is completely blank. What am i doing wrong? |
maylortaylor
, however my ‘category` (value being passed in) is completely blank.
Please show how the category is displayed in the View
I dont understand what you want?
I gave you my HTTPGet Controller method that creates the "Edit.html" page.
Once i run the project, go to the Edit page, the information pulls up fine on the screen. If i change the information (dropdown menu) and click the ‘save’ button. I am first redirected to the
CategoriesEditViewModel where it goes to the "Public Sub New()" empty constructor and then it goes to my HTTPPost Edit_Post ActionResult on my
CategoriesController.
Once it does get to the HTTPPost ActionResult, the value being passed in ("category As
CategoriesEditViewModel") has the following info:
Categories = Nothing
Category = nothing
Lists = nothing
SelectedItem = nothing
When Posts return "blank" stuff for fields even though the original View sent out showed something or the user changed a value….. the root cause is related to how MVC works and how the data was posted back.
- MVC doesn’t really pass strong typed objects back and forth, rather is passes querystrings as posts or gets. Query strings are just Name/Value pairs…
- When a post happens MVC will try to match the ActionMethod’s parameter type. It does this by first creating a null instance of that type and then it tries to "plug" the values with the query string values posted back. This
means that the "Name" of the query string values becomes highly important. If MVC cannot find the name of something in the object it creates in the query string it correctly does nothing! No errors, nothing, it just leaves those things as they were at creation. - The symptom could be "I see it in the view" but can’t see it as soon as it gets back to my controller’s action method…
- Solution: take a very close look after the original view is created what names are given to the html tags and make sure your begin-form includes them on the post back. Otherwise you will think you are "missing data".
This is the html being created for my ‘EditFor()’
<input class="text-box single-line" id="Category_codDescr" name="Category.codDescr" type="text" value="Academic" />
On the action method handling the post put a break point at first line and tell us what values are being passed in. You should be able to locate one for Category_codDescr. If you can’t then the form isn’t posting this back properly.
just wanted to see the html that is generated by the
@Using Html.BeginForm() @<fieldset> @Html.EditorFor(Function(m) m.Category.codDescr) @Html.DropDownListFor(Function(m) m.SelectedItem, New SelectList(Model.lists.organizationClubWaivers, "waiverId", "waiverName", Model.lists.organizationClubWaivers)) <input type="submit" value="Save" /> </fieldset>
End Using
Also, please tell what are Request.Form collection variables in the POST Action
maylortaylor
I dont understand what you want?
I gave you my HTTPGet Controller method that creates the "Edit.html" page.
Once i run the project, go to the Edit page, the information pulls up fine on the screen. If i change the information (dropdown menu) and click the ‘save’ button. I am first redirected to the
CategoriesEditViewModel where it goes to the "Public Sub New()" empty constructor and then it goes to my HTTPPost Edit_Post ActionResult on my
CategoriesController.Once it does get to the HTTPPost ActionResult, the value being passed in ("category As
CategoriesEditViewModel") has the following info:Categories = Nothing
Category = nothing
Lists = nothing
SelectedItem = nothing
show the Category class. what the binder is trying to do with the post data is:
var model = new CategoriesEditViewModel(); model.Category = new cihCategoryOrgDef(); model.Category.codDescr = Request.Form["Category.codDescr"]; // will add convert to type code