Bongo
Bongo

Reputation: 3153

MVC dictionary in EditorTemplate

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");
    }
}

Upvotes: 2

Views: 313

Answers (2)

Neel
Neel

Reputation: 11741

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/

Upvotes: 2

Quentin Roger
Quentin Roger

Reputation: 6538

Try to pass a model to your view.

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

Upvotes: 1

Related Questions