76820

EditorTemplate + Model not being returned on POST

Question:

I can't seem to figure this out, it is driving me crazy!

Essentially, I have a list of rows I need to display with one drop down list per row.

I have a view model:

public class UserMembershipViewModel:BaseViewModel { public List<ProgramMembership> ProgramMembership { get; set; } }

In my parent view I have, as you can see I am using an editor template which is located in "/Views/Shared/EditorTemplates/ProgramMembership.cshtml":

@using AcnCS.Model @model AcnCS.Model.ViewModels.User.UserMembershipViewModel @{ ViewBag.PageHeader = "Membership for " + Model.User.FullName; ViewBag.PageTitle = "Membership for " + @Model.User.FullName; ViewBag.HideNav = true; } @if (Model.ProgramMembership != null) { <div class="row-fluid"> <div class="span12"> <div id="permissions"> @using (Html.BeginForm()) { <table class="table table-bordered"> <thead> <tr> <td>Program</td> <td>Effective Membership?</td> <td>Permission Type</td> </tr> </thead> @Html.EditorFor(m => Model.ProgramMembership, "ProgramMembership") </table> <input type="submit" class="btn btn-primary" value="Save Changes"/> } </div> </div> </div> }

My Editor template (ProgramMembership.cshtml) is:

@using AcnCS.Model @model List<AcnCS.Model.ProgramMembership> @foreach(ProgramMembership membership in Model) { <tr> <td>@membership.ProgramName</td> <td> @if (membership.IsMember) { <span class="label label-success">@membership.IsMember</span> } else { @membership.IsMember } </td> <td>@Html.DropDownListFor(x => membership.PermissionType, membership.PermissionTypes)</td> </tr> }

Everything is being displayed properly, but when I submit, my model object is null, even the ProgramMembership property in the model is null:

[HttpPost] public ActionResult Membership(UserMembershipViewModel model) { // model IS NULL!! return View(model); }

Any help would be greatly appreciated!

Answer1:

I would pluralize the Property name since it is a collection, for better readability

public class UserMembershipViewModel:BaseViewModel { public List<ProgramMembership> ProgramMemberships { get; set; } }

and you dont need a Loop inside your EditorTemplate file

@model AcnCS.Model.ProgramMembership <tr> <td>@membership.ProgramName</td> <td> @if (membership.IsMember) { <span class="label label-success">@membership.IsMember</span> } else { @membership.IsMember } </td> <td>@Html.DropDownListFor(x => membership.PermissionType, membership.PermissionTypes)</td> </tr>

In your main view,call your EditorTemplate like this

@Html.EditorFor(m=>m.ProgramMemberships)

Recommend

  • php on submit redirect to another page
  • How to access model property in Razor view of IEnumerable Type?
  • PHP Sessions: Issue with back button
  • Special characters (like Ü,Ä,Ö) are not correctly displayed at runtime in case of using Template10
  • Using sed with the EDITOR variable in a script for virsh or sudoers
  • Link_to remote => true causing strange routing problems, Rails 3
  • ItemsControl only shows first Item of each object
  • Attaching to processes in C
  • Resuse ViewModel in Asp.net MVC
  • Returning multiple rows from a single row
  • ASP.Net MVC adding dynamic EditorFor elements
  • Unclear on how to implement dependency injection
  • iOS sharing data between viewcontrollers
  • How do you cancel someone's persistent cookie if their membership is no longer valid?
  • pass C# parameters to JS
  • set onClickListener in getview() with ViewHolder class
  • How can jQuery DataTables be applied to AJAX rendered partial view in MVC4?
  • How do I invalidate a bad authentication cookie early in the request?
  • Python. How to optimize search functions
  • Vim key mapping works in command editor, not in .vimrc - why?
  • Setting default value to Html.DropDownListFor
  • simplemembership MVC4 get username by userId
  • XAML UWP Flyout positioning
  • Accessing the property of a model in ASP.NET MVC View
  • Accesing properties in a UserControl from the MainWindow (WPF/MVVM)
  • MongoDB ObjectIds exposure
  • Neo4j cypher query for X-chromosome ancestors
  • How would you audit ASP.NET Membership tables, while recording what user made the changes?
  • SQL how to do an outer join properly
  • How would i implement an infinite scroll in my DOM
  • Adaptive Triggers in Resource Dictionary
  • EditFor for List of string
  • php is_file always return false
  • How to Save JSON data to SQL server database in C#?
  • Angular 2 constructor injection vs direct access
  • Turn off referential integrity in Derby? is it possible?
  • Authorize attributes not working in MVC 4
  • Busy indicator not showing up in wpf window [duplicate]
  • Python/Django TangoWithDjango Models and Databases
  • Net Present Value in Excel for Grouped Recurring CF