ado.net mvc3 元组在模型和单个视图中使用 [英] ado.net mvc3 tuple using in model and single views
问题描述
我有以下 ADO 模型
学生身份证、姓名和课程Id,Name,Student_ID
我为它做了如下视图
@model 元组
它的控制器如下
public ActionResult Create(){返回视图();}////POST:/Default3/创建[HttpPost]public ActionResult Create(Tuplet){尝试{//TODO: 在此处添加插入逻辑db.Students.AddObject(t.Item1);db.SaveChanges();t.Item2.S_ID = t.Item1.Id;db.Courses.AddObject(t.Item2);db.SaveChanges();return RedirectToAction("复制");}抓住{返回视图();}}
但是当我点击创建按钮时,它会给出以下错误
<块引用><块引用>/"应用程序中的服务器错误.
没有为此对象定义无参数构造函数.
Tuple<X, Y>
类没有默认构造函数,所以如果你想让它工作,你需要编写一个自定义模型绑定器.另一种可能性是使用我建议您使用的自定义视图模型:
公共类 MyViewModel{公共课程课程{得到;放;}公共学生学生{得到;放;}}
然后:
public ActionResult Create(){返回视图(新的 MyViewModel());}////POST:/Default3/创建[HttpPost]公共 ActionResult 创建(MyViewModel 模型){尝试{//TODO: 在此处添加插入逻辑db.Students.AddObject(t.Student);db.SaveChanges();t.Course.S_ID = t.Student.Id;db.Courses.AddObject(t.Course);db.SaveChanges();return RedirectToAction("复制");}抓住{返回视图(模型);}}
最后:
@model MvcApplication4.Models.MyViewModel@{ViewBag.Title = "创建";}<h2>创建</h2>@using (Html.BeginForm()) {@Html.ValidationSummary(true)<字段集><传奇>课程</传奇><div class="编辑器标签">@Html.LabelFor(model => model.Student.Name)</div><div class="编辑器字段">@Html.EditorFor(model => model.Student.Name)@Html.ValidationMessageFor(model => model.Student.Name)</div><div class="编辑器标签">@Html.LabelFor(model => model.Student.S_ID, "学生")</div><字段集><传奇>学生</传奇><div class="编辑器标签">@Html.LabelFor(model => model.Course.Name)</div><div class="编辑器字段">@Html.EditorFor(model => model.Course.Name)@Html.ValidationMessageFor(model => model.Course.Name)</div><div class="编辑器标签">@Html.LabelFor(model => model.Course.Class)</div><div class="编辑器字段">@Html.EditorFor(model => model.Course.Class)@Html.ValidationMessageFor(model => model.Course.Class)</div><p><输入类型=提交"值=创建"/></p></字段集>}
I have the following ADO Model
Student Id,Name and Course Id,Name,Student_ID
I have made the following view for it
@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>
}
And the controller for it is as
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();
}
}
But When i click the Creat button it gives the following Error
Server Error in '/' Application.
No parameterless constructor defined for this object.
The Tuple<X, Y>
class doesn't have a default constructor so you will need to write a custom model binder if you want this to work. Another possibility is to use a custom view model which is what I would recommend you:
public class MyViewModel
{
public Course Course { get; set; }
public Student Student { get; set; }
}
and then:
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);
}
}
and finally:
@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>
}
这篇关于ado.net mvc3 元组在模型和单个视图中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!