You must Sign In to post a response.
  • Category: ASP.Net MVC

    Error of 'System.Collections.Generic.List'

    Hi,

    I am trying getting value from model and display into edit form.
    I want data updated..but i am getting following error..
    The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[Mvc.Models.mvcProductItem]', but this dictionary requires a model item of type 'Mvc.Models.NewProductItem'.

    ccode :

    public ActionResult InsertOrEdit(int id = 0)
    {
    try
    {
    if (id == 0)
    {
    SetCategory();
    return View();
    }
    else
    {
    SetCategory();
    //HttpResponseMessage response = GlobalVariables.webApiClient.GetAsync("ProductItem/" + id.ToString()).Result;
    //return View(response.Content.ReadAsAsync<mvcProductItem>().Result);

    IEnumerable<mvcProductItem> prodList;
    HttpResponseMessage response = GlobalVariables.webApiClient.GetAsync("ProductItem/" + id.ToString()).Result;
    prodList = response.Content.ReadAsAsync<IEnumerable<mvcProductItem>>().Result;

    List<mvcProductItem> result = new List<mvcProductItem>();
    result = prodList.ToList<mvcProductItem>();

    return View("InsertOrEdit", result.ToList());
    }
    }
    catch (Exception ex)
    {
    throw ex;
    }
    }

    view
    ===

    @model Mvc.Models.mvcProductItem

    @{
    ViewBag.Title = "InsertOrEdit";
    }
    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script src="~/Scripts/jquery-1.10.2.js"></script>
    @section Scripts{
    <script type="text/javascript">
    function GetSubCategory() {
    var categoryID = $("#ddlCategory").val();
    $.ajax({
    url: "SetSubCategory",
    type: "POST",
    datatype: "application/json",
    contentType: "application/json",
    data: JSON.stringify({
    categoryID: +categoryID
    }),
    success: function (result) {
    $("#ddlSubCategory").html("");
    $("#ddlSubCategory").append('<option value="' + 0 + '">'
    + 'Select SubCategory' + '</option>');
    $.each(result, function (i, result) {
    $("#ddlSubCategory").append('<option value="' + result.Value + '">'
    + result.Text + '</option>');
    })
    },
    error: function (ex) {
    console.log(ex);
    alert("There is no Subcategories");
    },
    });
    }
    </script>
    @Scripts.Render("~/bundles/jqueryval");
    }

    @using (Html.BeginForm())
    {
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
    <h2>Product Item</h2>
    <hr />
    @Html.ValidationSummary(true)

    <div class="form-group" style="text-align:left;">
    @Html.Label("Product Name", new { @class = "control-label col-md-2" })
    <div class="col-md-10">
    @Html.EditorFor(model => model.NAME)
    @Html.ValidationMessageFor(model => model.NAME)
    </div>
    </div>

    <div class="form-group" style="text-align:left;">
    @Html.Label("Product Number", new { @class = "control-label col-md-2" })
    <div class="col-md-10">
    @Html.EditorFor(model => model.NUMBER)
    @Html.ValidationMessageFor(model => model.NUMBER)
    </div>
    </div>

    <div class="form-group" style="text-align:left;">
    @Html.Label("Category", new { @class = "control-label col-md-2" })
    <div class="col-md-10">
    @Html.DropDownListFor(model => model.CATEGORY_ID, new SelectList(ViewBag.ListOfCategories, "CATEGORY_ID", "CATEGORY"), "Select Category", new { id = "ddlCategory", @class = "form-control", onchange = "GetSubCategory();", style = "font-weight:bold;" })
    @Html.ValidationMessageFor(model => model.CATEGORY_ID)
    </div>
    </div>

    <div class="form-group" style="text-align:left;">
    @Html.Label("SubCategory", new { @class = "control-label col-md-2" })
    <div class="col-md-10">
    @Html.DropDownListFor(model => model.SUBCATEGORY_ID, new SelectList(Enumerable.Empty<SelectListItem>(), "SUBCATEGORY_ID", "SUBCATEGORY"),
    new { id = "ddlSubCategory", @class = "form-control", style = "font-weight:bold;" })
    @Html.ValidationMessageFor(model => model.SUBCATEGORY_ID)
    </div>
    </div>

    <div class="form-group" style="text-align:left;">
    @Html.Label("Model Name", new { @class = "control-label col-md-2" })
    <div class="col-md-10">
    @Html.EditorFor(model => model.Model)
    @Html.ValidationMessageFor(model => model.Model)
    </div>
    </div>

    <div class="form-group" style="text-align:left;">
    @Html.Label("Starndar Cose", new { @class = "control-label col-md-2" })
    <div class="col-md-10">
    @Html.EditorFor(model => model.Standard_Cost)
    @Html.ValidationMessageFor(model => model.Standard_Cost)
    </div>
    </div>

    <div class="form-group" style="text-align:left;">
    @Html.Label("List Price", new { @class = "control-label col-md-2" })
    <div class="col-md-10">
    @Html.EditorFor(model => model.List_Price)
    @Html.ValidationMessageFor(model => model.List_Price)
    </div>
    </div>
    <div class="form-group">
    <div class="col-md-offset-2 col-md-10">
    <input type="submit" value="Create" class="btn btn-primary" />
    </div>
    </div>
    </div>
    }

    <div>
    @Html.ActionLink("Back to List", "Index")
    </div>
  • #769456
    Hi Chandrasekar,

    When you edit you will get only one record by passing some id, so why you are sending list to that edit view?
    Change the code something like this.

    mvcProductItem result = new mvcProductItem();
    result = GetRecord(ID);//single object
    return View("InsertOrEdit", result);

    If you are passing object to view then view should be like below

    view
    ===
    @model Mvc.Models.mvcProductItem

    If you are passing list of object to view then view should be like below

    view
    ===
    @model IEnumerable<Mvc.Models.mvcProductItem>

    Hope this solves your issue.

    Sridhar Thota.
    Editor: DNS Forum.


  • Sign In to post your comments