0
votes

I am using mv3, iam creating applicationin which require when i click on create button data is submitted to database and list below the button is get updated.

i have tried partial view also but it shows error :

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[MvcStudent.stu]', but this dictionary requires a model item of type 'MvcStudent.Models.StuModel'.

i am listing my code below plz help

StudentController.cs

    public class StudentController : Controller
    {
        //
        // GET: /Student/
        stdataDataContext stdb = new stdataDataContext();
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult create()
        {

            return View(stdb.stus.ToList());
        }

        [HttpPost]
        public ActionResult create(MvcStudent.Models.StuModel stu)
        {

            stu student = new stu();
            student.name = stu.name;
            student.address = stu.addr;
            stdb.stus.InsertOnSubmit(student);
            stdb.SubmitChanges();

            return View();

        }

    }

_Create.cshtml

    @model MvcStudent.Models.StuModel

    @{
        ViewBag.Title = "Create";
    }

    <h2>Create</h2>

    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" typ

e="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>StuModel</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.name)
            @Html.ValidationMessageFor(model => model.name)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.addr)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.addr)
            @Html.ValidationMessageFor(model => model.addr)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.gen)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.gen)
            @Html.ValidationMessageFor(model => model.gen)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

@Html.Partial("_PartialGrid");

_PartialGrid.cshtml

    @model IEnumerable<MvcStudent.Models.StuModel>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            name
        </th>
        <th>
            addr
        </th>
        <th>
            gen
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.addr)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.gen)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
        </td>
    </tr>
}

</table>
1

1 Answers

1
votes

The error tells you what you need to know. Look at it again

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[MvcStudent.stu]', but this dictionary requires a model item of type 'MvcStudent.Models.StuModel'.

You are passing an object of type System.Collections.Generic.List`1[MvcStudent.stu] from your controller to your view. Your view is expecting an object of type MvcStudent.Models.StuModel.

Look again at your controller -

public ActionResult create()
{
    return View(stdb.stus.ToList());
}

Because you have not specified a view, it uses the name of the action to choose which View to try to load - so in this case: Create. Your 'Create' view contains this line at the top

@model MvcStudent.Models.StuModel

This declares you must pass an object of that Type for the view to load.

That explains the error you are getting - it seems to me like you are mixing a 'create' view with a 'list' view. Perhaps consider separating them out.