本文介绍了EditorTemplate +型号没有对POST返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法想出解决办法,这是快把我逼疯了!

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.

我有一个视图模型:

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

在我父视图我有,你可以看到我使用它位于/Views/Shared/EditorTemplates/ProgramMembership.cshtml编辑模板:

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>
    }

我的编辑模板(ProgramMembership.cshtml)是:

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>
}

显示正确的一切,但是当我提出,我的模型对象为空,甚至在模型中的ProgramMembership属性为null:

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);
    }

任何帮助将大大AP preciated!

Any help would be greatly appreciated!

推荐答案

我的复数属性名,因为它是一个集合,为更好的可读性

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

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

和你不需要你EditorTemplate文件中环路

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>

在你的主视图,请致电EditorTemplate这样

In your main view,call your EditorTemplate like this

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

这篇关于EditorTemplate +型号没有对POST返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 16:43