ado.net mvc3 tuple using in model and single views

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>
}

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 need to pass the model to your view. Example:

return View(model);