【问题标题】:MVC dictionary in EditorTemplateEditorTemplate 中的 MVC 字典
【发布时间】:2016-03-28 19:08:12
【问题描述】:

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

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

第一个模型:

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 ?

顺便说一句,控制器非常小,但为了完整起见,它来了

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

【问题讨论】:

  • 您是否将任何内容传递给您的视图?
  • 不,我以为模型是由@model Web.Models.Structures.QuantityAndSize 自动绑定的
  • @Bongo 它会自动绑定。但是你应该填写它。像这样:ViewData.Model = new NewItemDeliveryModel(); 在您的View() 通话之前。你应该在 Controller 中初始化你的字典。
  • 谢谢你。那是正确的答案
  • @Neel 是第一个 =) 但你总是可以投票给其他答案

标签: c# asp.net asp.net-mvc asp.net-mvc-4 dictionary


【解决方案1】:

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

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

【讨论】:

  • 很高兴它帮助了@Bongo :)
【解决方案2】:

尝试将模型传递给您的视图。

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

【讨论】:

    猜你喜欢
    • 2021-06-04
    • 2015-06-22
    • 1970-01-01
    • 2011-06-26
    • 1970-01-01
    • 1970-01-01
    • 2019-02-03
    • 2021-06-13
    • 2011-01-19
    相关资源
    最近更新 更多