ado.net mvc3元组在模型和单个视图中使用

Muh*_*man 6 tuples asp.net-mvc-3

我有以下ADO模型

学生 ID,姓名课程 ID,姓名,Student_ID

我已经为它做了以下观点

@model Tuple<MvcApplication4.Models.Course, MvcApplication4.Models.Student >
@{
    ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Course</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Item1.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Item1.Name)
            @Html.ValidationMessageFor(model => model.Item1.Name)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Item1.S_ID, "Student")
        </div>
            <fieldset>
        <legend>Student</legend>

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

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

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

    </fieldset>
}
Run Code Online (Sandbox Code Playgroud)

它的控制器是

public ActionResult Create()
{
      return View();
} 

//
// POST: /Default3/Create

[HttpPost]
public ActionResult Create(Tuple<Student ,Course > t)
{

    try
    {
        // TODO: Add insert logic here

        db.Students.AddObject(t.Item1);
        db.SaveChanges();

        t.Item2.S_ID = t.Item1.Id;
        db.Courses.AddObject(t.Item2);
        db.SaveChanges();

        return RedirectToAction("Copy");
    }
    catch
    {
        return View();
    }
}
Run Code Online (Sandbox Code Playgroud)

但是当我单击"创建"按钮时,它会出现以下错误

'/'应用程序中的服务器错误.

没有为此对象定义的无参数构造函数.

Dar*_*rov 11

Tuple<X, Y>类没有默认的构造函数,所以你需要编写一个自定义的模型绑定,如果你想这个工作.另一种可能性是使用我建议你的自定义视图模型:

public class MyViewModel
{
    public Course Course { get; set; }
    public Student Student { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

然后:

public ActionResult Create()
{
    return View(new MyViewModel());
} 

//
// POST: /Default3/Create

[HttpPost]
public ActionResult Create(MyViewModel model)
{
    try
    {
        // TODO: Add insert logic here
        db.Students.AddObject(t.Student);
        db.SaveChanges();

        t.Course.S_ID = t.Student.Id;
        db.Courses.AddObject(t.Course);
        db.SaveChanges();

        return RedirectToAction("Copy");
    }
    catch
    {
        return View(model);
    }
}
Run Code Online (Sandbox Code Playgroud)

最后:

@model MvcApplication4.Models.MyViewModel
@{
    ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Course</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Student.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Student.Name)
            @Html.ValidationMessageFor(model => model.Student.Name)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Student.S_ID, "Student")
        </div>
            <fieldset>
        <legend>Student</legend>

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

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

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}
Run Code Online (Sandbox Code Playgroud)


Eri*_*sch 2

MVC 非常智能,但它无法真正弄清楚如何创建 Tuple 的新实例并创建项目的新实例,然后为其分配适当的项目。这任务太复杂了。

您收到的错误是因为 Tuple 没有默认的无参数构造函数,并且需要在构造函数中将新项传递给它,这是 MVC 无法做到的。

您必须将其分解,并从包含您的项目作为成员的视图模型在控制器操作中创建元组。