EditorTemplate中的MVC字典

本文关键字:字典 MVC 中的 EditorTemplate | 更新日期: 2023-09-27 18:27:41

我不想将Dictionary绑定到View。但这个模型似乎是空的。棘手的部分是,我在另一个视图中使用了一个视图,但我不知道我的错误在哪里

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

第一种型号:

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,因为模型为Null。我对ASP.Net MVC还很陌生,问题本身看起来很简单,但我找不到错误的原因。所以我的问题是为什么我的模型等于Null?

顺便说一句,控制器很小,但为了完整起见,这里有

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

EditorTemplate中的MVC字典

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

// 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/

尝试将模型传递到视图中。

public class NewItemDeliveryController : Controller
{
// GET: NewItemDelivery
public ActionResult Index()
{
    var model = new NewItemDeliveryModel(){*Set your Dictionnary here*};
    return View("NewItemDelivery",model);
}
}