模型内的列表项在发布后始终为空-Asp.net MVC [英] List item inside model always null on post - Asp.net MVC

查看:76
本文介绍了模型内的列表项在发布后始终为空-Asp.net MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试发布包含 List<> 项目的模型,如下所示:

I am trying to post model which contains List<> item looks like below:

public class AddSubscriptionPlanModel
{
    public AddSubscriptionPlanModel()
    {
        AllFeatures = new List<Feature>();
    }
    public int Id { get; set; }
    public int SubscriptionPlanId { get; set; }
    public int SubscriptionId { get; set; }
    public string Name { get; set; }
    public bool IsActive { get; set; }
    [Display(Name = "No Of Users")]
    public int? NoOfUsers { get; set; }
    [Display(Name = "Duration Type")]
    public DurationType DurationType { get; set; }
    public int Duration { get; set; }
    public decimal Amount { get; set; }
    public int SubscriptionPlanTrailId { get; set; }
    public int FeatureId { get; set; }
    public DateTime CreatedDateUtc { get; set; }
    public DateTime LastUpdatedUtc { get; set; }
    public int CreatedBy { get; set; }
    public int LastUpdatedBy { get; set; }    
    public List<Feature> AllFeatures { get; set; }
}

我正在填充get方法上的所有必填字段,并从数据库中填充列表

I am populating all required fields on get method and feeding List from database

public ActionResult Mapping(int id)
{        
    var features = FeatureManager.GetAllFeatures();
    var model = new Ejyle.DevAccelerate.Web.App.Models.SubscriptionPlan.AddSubscriptionPlanModel();

    model.AllFeatures = features;
    model.Id = id;
    model.SubscriptionId = id;
    model.NoOfUsers = 20;
    model.SubscriptionPlanId = 1;
    model.SubscriptionPlanTrailId = 1;
    model.Amount = 1000;
    model.CreatedBy = 1;
    model.LastUpdatedBy = 1;
    model.FeatureId = 1;
    model.Duration = 20;

    return View(model);
}

我的查看代码如下:

@using (Html.BeginForm("Mapping", "SubscriptionPlans", FormMethod.Post))
{
    @Html.HiddenFor(model => model.Id)
    @Html.HiddenFor(model => model.Amount)
    @Html.HiddenFor(model => model.Duration)
    @Html.HiddenFor(model => model.FeatureId)
    @Html.HiddenFor(model => model.CreatedBy)
    @Html.HiddenFor(model => model.LastUpdatedBy)
    @Html.HiddenFor(model => model.NoOfUsers)
    @Html.HiddenFor(model => model.SubscriptionId)
    @Html.HiddenFor(model => model.SubscriptionPlanId)
    @Html.HiddenFor(model => model.SubscriptionPlanTrailId)
    @Html.HiddenFor(model => model.AllFeatures)
    <table class="table table-striped">
        <thead>
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.Name)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.IsActive)
                </th>
            </tr>
        </thead>
        <tbody>
            @for (int i=0; i < Model.AllFeatures.Count; i++)
            {
                <tr>
                    @Html.HiddenFor(model => model.AllFeatures[i].Id)
                    @Html.HiddenFor(model => model.AllFeatures[i].Name)
                    @Html.HiddenFor(model => model.AllFeatures[i].IsActive)
                    <td>
                        @Html.TextBoxFor(model => model.AllFeatures[i].Id)
                    </td>
                    <td>
                        @Html.TextBoxFor(model => model.AllFeatures[i].Name)
                    </td>
                    <td>
                        @Html.EditorFor(model => model.AllFeatures[i].IsActive)
                    </td>
                </tr>
            }
        </tbody>
    </table>
    <input type="submit" class="btn btn-success" value="Submit" name="Submit"/>
}

在View上,列表项正确呈现,甚至我也可以编辑这些字段.
但是在发布时,除 AllFeatures 列表项属性外,所有属性均具有值,该属性始终显示count = 0.

On View, the list items rendered properly and even I am able to edit those fields.
But On post, all properties have the value except AllFeatures list item property which always shows count = 0.

这是渲染功能时我的视图的样子

Edit 1: This is how my view looks like while rendering features

方法签名发布到:

[HttpPost]
public ActionResult Mapping(AddSubscriptionPlanModel model)
{       
}

推荐答案

但是,在发布时,所有属性都具有除AllFeatures列表项属性之外的值,该属性始终显示count = 0.

But On post, all properties have the value except AllFeatures list item property which always shows count = 0.

AllFeaturesFeature的通用列表.当您这样做时:

AllFeatures is a generic list of Feature. When you do this:

@Html.HiddenFor(model => model.AllFeatures) 

Razor视图引擎将呈现以下内容:

The Razor view engine will render this:

<input id="AllFeatures" 
       name="AllFeatures" 
       type="hidden" 
       value="System.Collections.Generic.List`1[Namespace.Feature]">
       <!--Where Namespace is the Namespace where Feature is defined. -->

换句话说,HiddenFor调用项目上的ToString()并将其简单地放入input标记的value属性中.

In other words, HiddenFor calls the ToString() on the item and simply puts that into the input tag's value attribute.

开机自检发生了什么

发布表单时,DefaultModelBinder正在寻找一个名为AllFeatures但类型为string的属性,以便它可以将其分配给它:

When you post the form, the DefaultModelBinder is looking for a property named AllFeatures but of type string so it can assign this to it:

System.Collections.Generic.List`1[Namespace.Feature]

它找不到一个,因为您的模型没有类型为stringAllFeatures,因此它只是忽略了它并绑定了所有其他属性.

It does not find one since your model does not have AllFeatures of type string, so it simply ignores it and binds all the other properties.

AllFeatures列表项属性,始终显示count = 0.

AllFeatures list item property which always shows count = 0.

是的,这不是您发布的AllFeatures列表,而是构造函数中的列表,显然是计数为0的空列表:

Yes, it will and this is not the AllFeatures list which you posted but the one from the constructor which clearly is an empty list with a count 0:

public AddSubscriptionPlanModel()
{
    AllFeatures = new List<Feature>();
}

我不确定为什么要将所有功能发送到客户端(浏览器),然后将其发布回服务器.

I am not sure why you are sending all the features to the client (browser) and then you need to post it back to the server.

解决方案

要解决此问题,只需删除以下行:

To fix the issue, simply remove this line:

@Html.HiddenFor(model => model.AllFeatures)

现在,在绑定期间它不会引起任何混乱,并且MVC会将循环中的项目绑定到AllFeatures属性.

Now it will not cause any confusion during binding and MVC will bind the items in the loop to the AllFeatures property.

事实上,据我所知,您真正需要的唯一代码是(如果出于其他原因需要隐藏字段,我可能是错误的.但是,如果您只希望用户编辑AllFeatures,您不需要任何隐藏字段):

In fact, the only code you really need, as far as I can tell from your question is this(I could be wrong if you need the hidden fields for some other reason. But if you just want the user to edit the AllFeatures, you do not need any of the hidden fields):

@for (int i = 0; i < Model.AllFeatures.Count; i++)
{
    <tr>
        <td>
            @Html.TextBoxFor(model => model.AllFeatures[i].Id)
        </td>
        <td>
            @Html.TextBoxFor(model => model.AllFeatures[i].Name)
        </td>
        <td>
            @Html.EditorFor(model => model.AllFeatures[i].IsActive)
        </td>
    </tr>
}

这篇关于模型内的列表项在发布后始终为空-Asp.net MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆