使用模型和单一观点ado.net MVC3元组 [英] ado.net mvc3 tuple using in model and single views
问题描述
我有以下ADO型号
学生
的标识,名称的
和
课程
的标识,名称,student_id数据的
我已经为它下面的视图
@model元组LT; MvcApplication4.Models.Course,MvcApplication4.Models.Student>
@ {
ViewBag.Title =创建;
}
< H2>创建< / H>
@using(Html.BeginForm()){
@ Html.ValidationSummary(真)
<&字段集GT;
<传奇>课程和LT; /传说> < DIV CLASS =编辑标记>
@ Html.LabelFor(型号=> model.Item1.Name)
< / DIV>
< DIV CLASS =主编场>
@ Html.EditorFor(型号=> model.Item1.Name)
@ Html.ValidationMessageFor(型号=> model.Item1.Name)
< / DIV>
< DIV CLASS =编辑标记>
@ Html.LabelFor(型号=> model.Item1.S_ID,学生)
< / DIV>
<&字段集GT;
<传奇>学生< /传说> < DIV CLASS =编辑标记>
@ Html.LabelFor(型号=> model.Item2.Name)
< / DIV>
< DIV CLASS =主编场>
@ Html.EditorFor(型号=> model.Item2.Name)
@ Html.ValidationMessageFor(型号=> model.Item2.Name)
< / DIV> < DIV CLASS =编辑标记>
@ Html.LabelFor(型号=> model.Item2.Class)
< / DIV>
< DIV CLASS =主编场>
@ Html.EditorFor(型号=> model.Item2.Class)
@ Html.ValidationMessageFor(型号=> model.Item2.Class)
< / DIV> &所述p为H.;
<输入类型=提交值=创建/>
&所述; / P>
< /字段集> < /字段集>
}
和它的控制器是为
公众的ActionResult的Create()
{
返回查看();
}//
// POST:/ Default3 /创建[HttpPost]
公众的ActionResult创建(元组LT,学生,课程和GT; T)
{ 尝试
{
// TODO:添加插入逻辑这里 db.Students.AddObject(t.Item1);
db.SaveChanges(); t.Item2.S_ID = t.Item1.Id;
db.Courses.AddObject(t.Item2);
db.SaveChanges(); 返回RedirectToAction(复制);
}
抓住
{
返回查看();
}
}
但是当我点击该按钮,科瑞它提供了以下错误
中的服务器错误'/'应用。
块引用>
此对象定义无参数的构造函数。
块引用>解决方案
元组LT; X,Y>
类没有默认的构造函数,所以你需要编写一个自定义模型绑定,如果你想这个工作。另一种可能性是使用的是一个自定义视图模型是什么,我会建议你:公共类MyViewModel
{
公共课程课程{搞定;组; }
公益助学学生{搞定;组; }
}和则:
公众的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(); 返回RedirectToAction(复制);
}
抓住
{
返回查看(模型);
}
}最后:
@model MvcApplication4.Models.MyViewModel
@ {
ViewBag.Title =创建;
}
< H2>创建< / H>
@using(Html.BeginForm()){
@ Html.ValidationSummary(真)
<&字段集GT;
<传奇>课程和LT; /传说> < DIV CLASS =编辑标记>
@ Html.LabelFor(型号=> model.Student.Name)
< / DIV>
< DIV CLASS =主编场>
@ Html.EditorFor(型号=> model.Student.Name)
@ Html.ValidationMessageFor(型号=> model.Student.Name)
< / DIV>
< DIV CLASS =编辑标记>
@ Html.LabelFor(型号=> model.Student.S_ID,学生)
< / DIV>
<&字段集GT;
<传奇>学生< /传说> < DIV CLASS =编辑标记>
@ Html.LabelFor(型号=> model.Course.Name)
< / DIV>
< DIV CLASS =主编场>
@ Html.EditorFor(型号=> model.Course.Name)
@ Html.ValidationMessageFor(型号=> model.Course.Name)
< / DIV> < DIV CLASS =编辑标记>
@ Html.LabelFor(型号=> model.Course.Class)
< / DIV>
< DIV CLASS =主编场>
@ Html.EditorFor(型号=> model.Course.Class)
@ Html.ValidationMessageFor(型号=> model.Course.Class)
< / DIV> &所述p为H.;
<输入类型=提交值=创建/>
&所述; / 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屋!