asp.net mvc and multiple models and modelbinders
I wanna try to make this is as sim开发者_如何学编程ple as possible.
Lets say I have a Project Model and a Task Model
I want to create a Project with 3 tasks assigned to that project in one single form
Whats the best way to do this??
Would the method simply receive a Project or what else do i need to have there.. will just saving the project (in repository) also save the related tasks?... In the view... do i need a viewModel.. Im confused. please help
public ActionResult Create(Project p){
}
Here's how I would proceed:
public class TaskViewModel
{
public string Name { get; set; }
}
public class ProjectViewModel
{
public string ProjectName { get; set; }
public IEnumerable<TaskViewModel> Tasks { get; set; }
}
then have a controller:
public class ProjectsController: Controller
{
public ActionResult Index()
{
var project = new ProjectViewModel
{
// Fill the collection with 3 tasks
Tasks = Enumerable.Range(1, 3).Select(x => new TaskViewModel())
};
return View(project);
}
[HttpPost]
public ActionResult Index(ProjectViewModel project)
{
if (!ModelState.IsValid)
{
// The user didn't fill all required fields =>
// redisplay the form with validation error messages
return View(project);
}
// TODO: do something with the model
// You could use AutoMapper here to map
// the view model back to a model which you
// would then pass to your repository for persisting or whatever
// redirect to some success action
return RedirectToAction("Success", "Home");
}
}
and then the view (~/Views/Projects/Create.cshtml
):
@model AppName.Models.ProjectViewModel
@using (Html.BeginForm())
{
<div>
@Html.LabelFor(x => x.ProjectName)
@Html.EditorFor(x => x.ProjectName)
@Html.ValidationMessageFor(x => x.ProjectName)
</div>
@Html.EditorFor(x => x.Tasks)
<input type="submit" value="Create!" />
}
and the corresponding task editor template (~/Views/Projects/EditorTemplates/TaskViewModel.cshtml
):
@model AppName.Models.TaskViewModel
<div>
@Html.LabelFor(x => x.Name)
@Html.EditorFor(x => x.Name)
@Html.ValidationMessageFor(x => x.Name)
</div>
Add a collection of Task
models to the Project
model, and use a foreach
loop to display the tasks, or repeat a partial view that knows how to display a single task.
精彩评论