C# EditorTemplate中的MVC字典
我不想把字典和视图绑定在一起。但该模型似乎是无效的。 棘手的是,我在另一个视图中使用了一个视图,但我不知道我的错误在哪里 以下是我目前的代码: 第一款车型:C# EditorTemplate中的MVC字典,c#,asp.net,asp.net-mvc,asp.net-mvc-4,dictionary,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,Dictionary,我不想把字典和视图绑定在一起。但该模型似乎是无效的。 棘手的是,我在另一个视图中使用了一个视图,但我不知道我的错误在哪里 以下是我目前的代码: 第一款车型: public class QuantityAndSize { private Dictionary<String, int> m_Size; public decimal Quantity {get;set;} public Dictionary<string,int> Size
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;
}
}
我在这里看到了NewItemDeliveryModel的视图
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
}
@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>
}
尝试将模型传递给视图
public class NewItemDeliveryController : Controller
{
// GET: NewItemDelivery
public ActionResult Index()
{
var model = new NewItemDeliveryModel(){*Set your Dictionnary here*};
return View("NewItemDelivery",model);
}
}
在此,您需要将模型传递到视图中,如下所示:
// GET: NewItemDelivery
public ActionResult Index()
{
var myModel = new NewItemDeliveryModel();
return View("NewItemDelivery", myModel);
}
旁注:从MVC 6开始,您将能够直接向视图注入依赖性
public class NewItemDeliveryController : Controller
{
// GET: NewItemDelivery
public ActionResult Index()
{
var model = new NewItemDeliveryModel(){*Set your Dictionnary here*};
return View("NewItemDelivery",model);
}
}
更多信息请参见此处:您是否向视图传递了任何内容?没有,我以为该模型是由@model Web.Models.Structures自动绑定的。QuantityAndSize@Bongo它将自动绑定。但是你应该把它填满。如下所示:
ViewData.Model=newNewItemDeliveryModel()在调用View()之前执行code>。你应该在Controller中初始化你的字典。谢谢。这是正确的answer@Neelwas first=)但你总是可以胜过其他一些答案,很高兴它有帮助@Bongo:)
// GET: NewItemDelivery
public ActionResult Index()
{
var myModel = new NewItemDeliveryModel();
return View("NewItemDelivery", myModel);
}