Asp.net mvc 3 ado.net mvc3元组在模型和单个视图中使用
我有以下ADO模型 学生 身份证,姓名 和 课程 身份证,姓名,学生证 我对此有如下看法Asp.net mvc 3 ado.net mvc3元组在模型和单个视图中使用,asp.net-mvc-3,tuples,Asp.net Mvc 3,Tuples,我有以下ADO模型 学生 身份证,姓名 和 课程 身份证,姓名,学生证 我对此有如下看法 @model Tuple<MvcApplication4.Models.Course, MvcApplication4.Models.Student > @{ ViewBag.Title = "Create"; } <h2>Create</h2> @using (Html.BeginForm()) { @Html.ValidationSummary(tru
@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>
}
@模型元组
@{
ViewBag.Title=“创建”;
}
创造
@使用(Html.BeginForm()){
@Html.ValidationSummary(true)
课程
@LabelFor(model=>model.Item1.Name)
@EditorFor(model=>model.Item1.Name)
@Html.ValidationMessageFor(model=>model.Item1.Name)
@LabelFor(model=>model.Item1.S_ID,“学生”)
学生
@LabelFor(model=>model.Item2.Name)
@EditorFor(model=>model.Item2.Name)
@Html.ValidationMessageFor(model=>model.Item2.Name)
@LabelFor(model=>model.Item2.Class)
@EditorFor(model=>model.Item2.Class)
@Html.ValidationMessageFor(model=>model.Item2.Class)
}
其控制器如下所示:
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();
}
}
public ActionResult Create()
{
返回视图();
}
//
//POST:/Default3/Create
[HttpPost]
公共操作结果创建(元组t)
{
尝试
{
//TODO:在此处添加插入逻辑
db.Students.AddObject(t.Item1);
db.SaveChanges();
t、 Item2.S_ID=t.Item1.ID;
db.Courses.AddObject(t.Item2);
db.SaveChanges();
返回重定向到操作(“副本”);
}
抓住
{
返回视图();
}
}
但当我单击“创建”按钮时,会出现以下错误
“/”应用程序中出现服务器错误
没有为此对象定义无参数构造函数
MVC非常聪明,但它无法真正理解如何创建一个新的元组实例,创建新的项实例,然后为其分配适当的项。这任务太复杂了 您得到的错误是因为元组没有默认的无参数构造函数,并且需要在构造函数中向其传递新项,这是MVC无法做到的
您必须对此进行分解,并在控制器操作中从包含项目作为成员的viewmodel创建元组。您需要将模型传递给视图。 例如: 该类没有默认构造函数,因此如果您希望此功能正常工作,则需要编写自定义模型绑定器。另一种可能是使用自定义视图模型,我建议您:
public class MyViewModel
{
public Course Course { get; set; }
public Student Student { get; set; }
}
然后:
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);
}
}
最后:
@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>
}
@model mvcapapplication4.Models.MyViewModel
@{
ViewBag.Title=“创建”;
}
创造
@使用(Html.BeginForm()){
@Html.ValidationSummary(true)
课程
@LabelFor(model=>model.Student.Name)
@EditorFor(model=>model.Student.Name)
@Html.ValidationMessageFor(model=>model.Student.Name)
@LabelFor(model=>model.Student.S_ID,“学生”)
学生
@LabelFor(model=>model.Course.Name)
@EditorFor(model=>model.Course.Name)
@Html.ValidationMessageFor(model=>model.Course.Name)
@LabelFor(model=>model.Course.Class)
@EditorFor(model=>model.Course.Class)
@Html.ValidationMessageFor(model=>model.Course.Class)
}
这似乎已经解决了我的问题,作为替代方案,它现在正在工作:
[HttpGet]
public ActionResult Create()
{
Course course = new Course();
Student student = new Student();
var tuple = new Tuple<Course,Student>(course,student);
return View(tuple);
}
[HttpPost]
public ActionResult Create(Tuple<Course,Student> tuple){ do something ...}
[HttpGet]
公共操作结果创建()
{
课程=新课程();
学生=新生();
var tuple=新的tuple(课程,学生);
返回视图(元组);
}
[HttpPost]
公共操作结果创建(元组){do something…}
我尝试了其他几种方法,包括这里建议的一些方法,但没有解决问题。我发布这篇文章是为了帮助其他可能想使用Tuple的人,但只有在没有其他选择的情况下才可以使用它。经过几分钟的挖掘和思考,我才让它工作起来。下面是我所做工作的一个快速示例: 获取操作:
[HttpGet]
public ActionResult Update(int id = 0)
{
ProductDto product = _productService.FindByID(id);
SupplierDto supplier = _supplierService.FindByProductID(productId: product.ProductID);
return View(model: new Tuple<ProductDto, SupplierDto>(product, supplier));
}
查看:
[HttpPost]
public JsonResult Update(int id = 0, ProductDto Item1, SupplierDto Item2)
{
// Get the product name
string productName = Item1.ProductName;
// Get the supplier name
string supplierName = Item2.SupplierName;
...
return Json(new { success = true });
}
@model Tuple<ProductDto, SupplierDto>
@{
ViewBag.Title = "add title later ... ";
AjaxOptions options = new AjaxOptions { ... };
}
@using (Ajax.BeginForm("Update", "Product", options, htmlAttributes: new { @id = "update-form" }))
{
<fieldset>
<legend>Update Product</legend>
<div class="display-label">
@Html.LabelFor(model => model.Item1.ProductName)
</div>
<div class="display-field">
@Html.EditorFor(model => model.Item1.ProductName)
</div>
...
<div class="display-label">
@Html.LabelFor(model => model.Item2.SupplierName)
</div>
<div class="display-field">
@Html.EditorFor(model => model.Item2.SupplierName)
</div>
</fieldset>
<div class="submit-button">
<button type="submit" class="button">Update details</button>
<div>
}
@模型元组
@{
ViewBag.Title=“稍后添加标题…”;
AjaxOptions选项=新的AjaxOptions{…};
}
@使用(Ajax.BeginForm(“更新”、“产品”、选项、htmlAttributes:new{@id=“updateform”}))
{
更新产品
@Html.LabelFor(model=>model.Item1.ProductName)
@Html.EditorFor(model=>model.Item1.ProductName)
...
@LabelFor(model=>model.Item2.SupplierName)
@EditorFor(model=>model.Item2.SupplierName)
更新详细信息
}
您应该在参数中绑定前缀
控制器:
public ActionResult ThisMethod([Bind(Prefix = "Item1")] AccountViewModel model)
{
// toDo
}
视图:
@模型元组
@EditorFor(model=>model.Item1.Firstname)
@DarinDimitri的解决方案是正确的,但也有一种使用元组的方法。如果您只是更改下面的代码,您将得到元组模型
[HttpPost]
public ActionResult Create(Course Item1, Student Item2)
{
try
{
// TODO: Add insert logic here
db.Students.AddObject(Item2);
db.SaveChanges();
Item1.S_ID = Item2.Id;
db.Courses.AddObject(Item1);
db.SaveChanges();
return RedirectToAction("Copy");
}
catch
{
return View();
}
类似于此公共操作结果Create(){Tuple t=new Tuple(new Course(),new Student());返回视图(t);}类似于此公共操作结果Create(){Tuple t=new Tuple(new Course(),new Student());返回视图(t);}我使用的是使用ADO.NET的datafirst方法,我在其中创建了这个自定义模型,。我无法将其与模型上下文文件链接。它仍然有以下错误:“没有为此对象定义无参数构造函数。”
@model Tuple<AccountViewModel>
@Html.EditorFor(model => model.Item1.Firstname)
[HttpPost]
public ActionResult Create(Course Item1, Student Item2)
{
try
{
// TODO: Add insert logic here
db.Students.AddObject(Item2);
db.SaveChanges();
Item1.S_ID = Item2.Id;
db.Courses.AddObject(Item1);
db.SaveChanges();
return RedirectToAction("Copy");
}
catch
{
return View();
}