EditorTemplate中的MVC字典(MVC dictionary in EditorTemplate)

系统教程 行业动态 更新时间:2024-06-14 17:02:18
EditorTemplate中的MVC字典(MVC dictionary in EditorTemplate)

我不想将字典绑定到视图。 但该模型似乎是空的。 棘手的部分是我在另一个视图中使用视图,我无法弄清楚我的错误在哪里。

这是我到目前为止的代码:

第一种模式:

public class QuantityAndSize { private Dictionary<String, int> m_Size; public decimal Quantity {get;set;} public Dictionary<string,int> Size { get { return m_Size; } set { m_Size = value; } } public int SelectedItem {get;set;} public QuantityAndSize() { Quantity = 0; m_Size = new Dictionary<string, int>(); SelectedItem = 0; } }

第二个模型

public class NewItemDeliveryModel { #region - member - private string m_Subject; private QuantityAndSize m_ShortfallQuantity; #endregion #region - properties [Display(Name = "Betreff")] public string Subject { get { return m_Subject; } set { m_Subject = value; } } [Display(Name = "Fehlmenge")] public QuantityAndSize ShortfallQuantity { get { return m_ShortfallQuantity; } set { m_ShortfallQuantity = value; } } #endregion #region - ctor - public NewItemDeliveryModel() { m_ShortfallQuantity = new QuantityAndSize(); } #endregion }

在这里,我有一个NewItemDeliveryModel的视图

@model Web.Models.NewItemDeliveryModel @{ ViewBag.Title = "NewItemDelivery"; } <h2>NewItemDelivery</h2> @using (Html.BeginForm()) { <div class="form-horizontal"> <div class="form-group"> @Html.LabelFor(model => model.Subject, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Subject) @Html.ValidationMessageFor(model => model.Subject) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.ShortfallQuantity, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.ShortfallQuantity) @Html.ValidationMessageFor(model => model.ShortfallQuantity) </div> </div> </div> }

和QuantityAndSize的视图

@model Web.Models.Structures.QuantityAndSize <div class="form-group"> @Html.LabelFor(model => model.Quantity, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Quantity) @Html.ValidationMessageFor(model => model.Quantity) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Size, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.DropDownListFor(model => model.Size, new SelectList(Model.Size,"Key","Value"), "Sizes") //<-- Model.Size says that Model is null but i can't figure out why @Html.ValidationMessageFor(model => model.Size) </div> </div>

问题是,我无法将我的Dictionary绑定到Html.DropDownList,因为Model是Null。 我是ASP.Net MVC的新手,问题本身似乎很简单,但我找不到错误的原因。 所以我的问题是为什么我的模型等于Null?

BTW控制器非常小,但为了完整起见,它来了

public class NewItemDeliveryController : Controller { // GET: NewItemDelivery public ActionResult Index() { return View("NewItemDelivery"); } }

I wan't to bind a Dictionary to a View. But the model seems to be Null. The tricky part is that I use a view in another view and I can't figure out where my error is.

Here is my code so far:

First Model:

public class QuantityAndSize { private Dictionary<String, int> m_Size; public decimal Quantity {get;set;} public Dictionary<string,int> Size { get { return m_Size; } set { m_Size = value; } } public int SelectedItem {get;set;} public QuantityAndSize() { Quantity = 0; m_Size = new Dictionary<string, int>(); SelectedItem = 0; } }

Second Model

public class NewItemDeliveryModel { #region - member - private string m_Subject; private QuantityAndSize m_ShortfallQuantity; #endregion #region - properties [Display(Name = "Betreff")] public string Subject { get { return m_Subject; } set { m_Subject = value; } } [Display(Name = "Fehlmenge")] public QuantityAndSize ShortfallQuantity { get { return m_ShortfallQuantity; } set { m_ShortfallQuantity = value; } } #endregion #region - ctor - public NewItemDeliveryModel() { m_ShortfallQuantity = new QuantityAndSize(); } #endregion }

Here I have a the View for the NewItemDeliveryModel

@model Web.Models.NewItemDeliveryModel @{ ViewBag.Title = "NewItemDelivery"; } <h2>NewItemDelivery</h2> @using (Html.BeginForm()) { <div class="form-horizontal"> <div class="form-group"> @Html.LabelFor(model => model.Subject, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Subject) @Html.ValidationMessageFor(model => model.Subject) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.ShortfallQuantity, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.ShortfallQuantity) @Html.ValidationMessageFor(model => model.ShortfallQuantity) </div> </div> </div> }

and the View for the QuantityAndSize

@model Web.Models.Structures.QuantityAndSize <div class="form-group"> @Html.LabelFor(model => model.Quantity, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Quantity) @Html.ValidationMessageFor(model => model.Quantity) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Size, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.DropDownListFor(model => model.Size, new SelectList(Model.Size,"Key","Value"), "Sizes") //<-- Model.Size says that Model is null but i can't figure out why @Html.ValidationMessageFor(model => model.Size) </div> </div>

the Problem is, I can't bind my Dictionary to the Html.DropDownList because the Model is Null. I am fairly new to ASP.Net MVC and the problem itself seems simple but I can't find the cause for the error. So my question is why is my Model equal to Null ?

BTW the controller is very small but for the sake of completeness here it comes

public class NewItemDeliveryController : Controller { // GET: NewItemDelivery public ActionResult Index() { return View("NewItemDelivery"); } }

最满意答案

在这里,您需要将模型传递到视图中,如下所示:

// GET: NewItemDelivery public ActionResult Index() { var myModel = new NewItemDeliveryModel(); return View("NewItemDelivery", myModel); }

附注:从MVC 6开始,您可以直接为视图注入依赖性。

请点击此处了解更多信息: https : //neelbhatt40.wordpress.com/2015/08/14/inject-new-directive-of-mvc/

Here you need to pass your model into your view as below:

// GET: NewItemDelivery public ActionResult Index() { var myModel = new NewItemDeliveryModel(); return View("NewItemDelivery", myModel); }

Side Note: from MVC 6 onwards you would be able to directly inject dependancy to your view.

See here for more: https://neelbhatt40.wordpress.com/2015/08/14/inject-new-directive-of-mvc/

更多推荐

public,class,Model,QuantityAndSize,电脑培训,计算机培训,IT培训"/> <meta name=

本文发布于:2023-04-21 18:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/5b2ef00499387b2ab13f719bfc956362.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字典   EditorTemplate   MVC   dictionary

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!