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.
MVC is pretty smart, but it can't really figure out how to create a new instance of Tuple and create new instances of the items, then assign the proper items to it. That's just too complex of a task.
The error you get is because a Tuple doesn't have a default parameterless constructor, and requires new items to be passed to it in the constructor, something that MVC can't do.
You will have to break this down, and create your tuple in your controller action from a viewmodel that contains your items as members.
You should bind prefix in parameters Controller:
View:
@DarinDimitri's solution is right but there is a way with Tuple as well. If you just change these code below you will get tuple models.
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:and then:
and finally:
This seems to have resolved the issue for me as an alternative and it is working now:
I tried several other approaches include some that have been suggested here but, didn't resolve the problem. I just posted this to help someone else who might want to use Tuple though, use it only if you don't have another alternative.
I got it to work after a few minutes of digging and some thinking. Here's a quick example of what I did:
GET action:
POST action:
View: