Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 如何在MVC3中添加列表并在同一视图中创建脚手架?_Asp.net Mvc 3_Partial Views - Fatal编程技术网

Asp.net mvc 3 如何在MVC3中添加列表并在同一视图中创建脚手架?

Asp.net mvc 3 如何在MVC3中添加列表并在同一视图中创建脚手架?,asp.net-mvc-3,partial-views,Asp.net Mvc 3,Partial Views,我使用的是mv3,iam正在创建应用程序,当我单击“创建”按钮时,需要将数据提交到数据库,并更新按钮下方的列表 我也尝试过局部查看,但显示错误: 传递到字典中的模型项的类型为 'System.Collections.Generic.List'1[MvcStudent.stu]',但 字典需要“MvcStudent.Models.StuModel”类型的模型项 我在plz帮助下面列出了我的代码 StudentController.cs public class StudentControl

我使用的是mv3,iam正在创建应用程序,当我单击“创建”按钮时,需要将数据提交到数据库,并更新按钮下方的列表

我也尝试过局部查看,但显示错误:

传递到字典中的模型项的类型为 'System.Collections.Generic.List'1[MvcStudent.stu]',但 字典需要“MvcStudent.Models.StuModel”类型的模型项

我在plz帮助下面列出了我的代码

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();

        }

    }
\u 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");
    @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>
@model MvcStudent.Models.StuModel
@{
ViewBag.Title=“创建”;
}
创造
@使用(Html.BeginForm()){
@Html.ValidationSummary(true)
StuModel
@LabelFor(model=>model.name)
@EditorFor(model=>model.name)
@Html.ValidationMessageFor(model=>model.name)
@LabelFor(model=>model.addr)
@EditorFor(model=>model.addr)
@Html.ValidationMessageFor(model=>model.addr)
@LabelFor(model=>model.gen)
@EditorFor(model=>model.gen)
@Html.ValidationMessageFor(model=>model.gen)

} @Html.Partial(“_PartialGrid”);
\u PartialGrid.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");
    @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>
@model IEnumerable

@ActionLink(“新建”、“创建”)

名称 地址 消息 @foreach(模型中的var项目){ @DisplayFor(modelItem=>item.name) @DisplayFor(modelItem=>item.addr) @DisplayFor(modeleItem=>item.gen) @ActionLink(“编辑”,“编辑”,新的{/*id=item.PrimaryKey*/})| @ActionLink(“详细信息”,“详细信息”,新的{/*id=item.PrimaryKey*/})| @ActionLink(“删除”,“删除”,新的{/*id=item.PrimaryKey*/}) }
错误告诉您需要知道的内容。再看一遍

传入字典的模型项的类型为“System.Collections.Generic.List`1[MvcStudent.stu]”,但此字典需要类型为“MvcStudent.Models.StuModel”的模型项

您正在将类型为
System.Collections.Generic.List`1[MvcStudent.stu]
的对象从控制器传递到视图。您的视图需要类型为
MvcStudent.Models.StuModel
的对象

再看看你的控制器-

public ActionResult create()
{
    return View(stdb.stus.ToList());
}
因为您没有指定视图,所以它使用操作的名称来选择要尝试加载的视图-因此在本例中:
Create
。“创建”视图在顶部包含此行

@model MvcStudent.Models.StuModel
这声明必须传递该类型的对象才能加载视图


这就解释了您所遇到的错误——在我看来,您似乎将“创建”视图与“列表”视图混合在一起。也许考虑把它们分开。

我能做什么来保持在同一视图上创建和列表。我只想在表单加载和提交时从列表中的数据库中检索更新的数据。提交时,我正在将数据保存到数据库