【问题标题】:How to bind value from UI to controller with complex model in c# mvc如何在 c# mvc 中将值从 UI 绑定到具有复杂模型的控制器
【发布时间】:2020-08-12 08:45:59
【问题描述】:

我是 .net 的新手。我有这个模型,这几天给我带来了真正的麻烦。

DetailedRecordModel

public class DetailedRecordModel
    {
        public string RecordID { get; set; }
        public string EmployeeID { get; set; }
        public string CustomerID { get; set; }

        [DataType(DataType.Date)]
        public string InitDate { get; set; }

        [DataType(DataType.Date)]
        public string DeliveryDate { get; set; }
        public virtual ICollection<PurchaseDetail> detail{ get; set; }
}

PurchaseDetail

public class PurchaseDetail
    {
        public string ProductID { get; set; }
        public int Qty { get; set; }
        public double price { get; set; }
        public string RecordID { get; set; }

    }

控制器

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(DetailedRecordModel record)
        {
            if (ModelState.IsValid)
            {
            return View(record);
             
            }
                return RedirectToAction("ViewRecords");
        }

html


<div class="form-group">
            @Html.LabelFor(model => model.EmployeeID, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(model => model.EmployeeID, (IEnumerable<SelectListItem>)ViewData["sellistemp"])
                @Html.ValidationMessageFor(model => model.EmployeeID, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(model => model.CustomerID, (IEnumerable<SelectListItem>)ViewData["sellistcust"])
                @Html.ValidationMessageFor(model => model.CustomerID, "", new { @class = "text-danger" })
            </div>
        </div>
 <tr>
       <td style="display:none" id="Index0" name="detail.Index" value="0"></td>
       <td>1</td>
       <td id="ProductID" name="detail[0].ProductID" value="sp00002">sp00002</td>
       <td id="Qty" name="detail[0].Qty" value="12123">12123</td>
       <td id="price" name="detail[0].price" value="2312">2312</td>
</tr>
<tr>
       <td style="display:none" id="Index1" name="detail.Index" value="1"></td>
       <td>2</td>
       <td id="ProductID" name="detail[1].ProductID" value="sp00003">sp00003</td>
       <td id="Qty" name="detail[1].Qty" value="2323">2323</td>
       <td id="price" name="detail[1].price" value="3223">3223</td>
</tr>

对于 RecordID、EmployeeID、CustomerID、InitDate 和 DeliveryDate 将它们传递给控制器​​都很好,但是对于 &lt;PurchaseDetail&gt; detail,我总是得到 null。我该如何解决这个问题?

【问题讨论】:

    标签: c# .net model-view-controller


    【解决方案1】:

    您必须使用View(myModel)RedirectToAction("ViewRecords", record) 将模型传递给视图

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(DetailedRecordModel record)
        {
            if (ModelState.IsValid)
            {
                return RedirectToAction("ViewRecords", record);
             
            }
            return View(record);
        }
    
        public IActionResult ViewRecords(DetailedRecordModel model)
        {
            return View(model);
        }
    

    然后在视图中你可以像这里How to pass model in MVC view一样访问模型

    1. @model DetailedRecordModel; 的顶部添加模型的定义
    2. 添加模型后,您可以使用@Model(html 格式)或Model 在文件中的任何位置访问它

        @model DetailedRecordModel;
        @{
            ViewData["Title"] = "ViewRecords";
        }
    
        <h1>ViewRecords</h1>
    
        <div class="form-group">
            @Html.LabelFor(model => model.EmployeeID, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(model => model.EmployeeID, (IEnumerable<SelectListItem>)ViewData["sellistemp"])
                @Html.ValidationMessageFor(model => model.EmployeeID, "", new { @class = "text-danger" })
            </div>
        </div>
    
        <div class="form-group">
            @Html.LabelFor(model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(model => model.CustomerID, (IEnumerable<SelectListItem>)ViewData["sellistcust"])
                @Html.ValidationMessageFor(model => model.CustomerID, "", new { @class = "text-danger" })
            </div>
        </div>
    
        @foreach (var entry in Model.detail)
        {
            <tr>
                <td style="display:none" id="Index0" name="detail.Index" value="0"></td>
                <td>1</td>
                <td id="ProductID" name="@entry.ProductID" value="sp00002">sp00002</td>
                <td id="Qty" name="@entry.Qty" value="12123">12123</td>
                <td id="price" name="@entry.price" value="2312">2312</td>
            </tr>
        }

    【讨论】:

    • 你能说得更具体点吗?我真的不明白。第二个代码部分对我来说没有多大意义
    • '对象引用未设置为对象的实例。 Model.get 返回 null'。现在该网站以某种方式无法访问。
    • 你能发布你的控制器以前的样子吗?如果你只是复制了我的两个方法就行不通了,可能你错过了 ViewRecords 方法上面的 [HttpGet]
    • 好吧,看看下面,我找到了解决方案。当我将&lt;td id="price" name="detail[0].price" value="2312"&gt;2312&lt;/td&gt; 名称更改为嵌套对象的命名方式时,它(某些东西)会自动绑定值。例如:wrongname[0].pricenestedname[0].price。我其实是用javascript自动生成&lt;tr&gt;s,下面的代码只是演示。无论如何,感谢您提供帮助
    【解决方案2】:

    在绝望的两天后,我知道为了绑定值,我通过在模型PurchaseDetail 中声明一个对象DetailedRecordModel 找到了答案,我必须将每个&lt;input&gt; 标记的名称更改为@ 987654324@

    【讨论】:

      猜你喜欢
      • 2012-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 2013-05-29
      • 2016-03-07
      • 1970-01-01
      相关资源
      最近更新 更多