本文介绍了路过的IEnumerable或列表模式用来控制HttpPost的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的问题:

我的模型

public class SpesaTrasportoView
{ 
    public int SPESATRASPORTOVIEW_ID;
    public decimal PREZZO;
    public string DESCRIZIONE;
}

public class SpeseTrasportoView
{
    public List<MvcCart.Models.SpesaTrasportoView> SpeseTrasportoModello { get; set; }
    //public IList<string> Spese  { get; set; }
}

我的看法

@model MvcCart.Models.SpeseTrasportoView
@{
    ViewBag.Title = "Spese Trasporto";
    int counter = 0;
}

<div class="form-content">
<h2>Spese Trasporto</h2>
<p>
   Modifica e gestisci gli importi delle spese di trasporto.     
</p>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {

    <div class="data-content">
        <fieldset>
            <legend>Informazione Spese Trasporto</legend>
            @for (int i = 0; i < Model.SpeseTrasportoModello.Count; i++)
            {
                 <div class="editor-field">
                    @Html.TextBoxFor(m => m.SpeseTrasportoModello.ToList()[i].PREZZO)
                 </div>
            }


            <p>
               <input type="hidden" value="@ViewBag.FormStatus" id="Action" name="Action"/>
               <input type="submit" value="Salva" id="Salva" name="Salva"/>
            </p>
        </fieldset>
    </div>
}

</div>

我的控制器

[HttpPost]
public ActionResult SpeseTrasporto(SpeseTrasportoView model)
{
    //model.SpeseTrasportoModello is ever NULL :(((((
    return View();
}

当我提交model.SpeseTrasportoModello为空!为什么MVC3不绑定数据????

When I submit the model.SpeseTrasportoModello is null! why mvc3 don't bind the data????

推荐答案

有你需要修复的几件事情。首先为您的机型,让一切属性。不要使用领域。

There are a few things you need to fix. First for your models, make everything properties. Do not use fields.

模型:

public class SpesaTrasportoView
{
    public int SPESATRASPORTOVIEW_ID { get; set; }
    public decimal PREZZO { get; set; }
    public string DESCRIZIONE { get; set; }
}

public class SpeseTrasportoView
{
    public List<SpesaTrasportoView> SpeseTrasportoModello { get; set; }
}

现在,修改for循环中的视图,以便它看起来是这样的:

Now, change the for loop in the view so that it looks like this:

@for (int i = 0; i < Model.SpeseTrasportoModello.Count; i++)
{
    var item = Model.SpeseTrasportoModello[i];

    <div class="editor-field">
        <input type="text" 
               name="SpeseTrasportoModello[@i].PREZZO" 
               value="@item.PREZZO" />
    </div>
}

您需要了解的关键是输入的名称格式。它应该有一个是你的模型(列表)内用方括号里的索引,然后,这个投入是(例如preZZO)属性的名称属性的名称。

The key thing you need to know is the input name format. It should have the name of the property that is inside your model (your list) with an index in square brackets and then the name of the property that this input is for (e.g. PREZZO).

这篇关于路过的IEnumerable或列表模式用来控制HttpPost的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 18:43