Situation: In some project management software written on asp.net, I have a project project page (works fine). I need to add to this the ability to add tasks from the list of templates to this preliminary creation of the project, but the list of available tasks depends on some values that are in the creation form.
My abstract solution is this:
- I have a Create view and an Add Tasks view - both are strongly typed in a composite viewModel defined in the controller
- My Create method checks which button was used to call it - if the Add Tasks button then displays the AddTasks view, passing the model from the create view, again all in one controller.
- AddTasks View messages for the Create view with one of two buttons, one loads the view and the other causes the database to be actually saved.
My problem is this:
- In different views, different properties of the same model are used, but when transferring this model between them, the data is reset (in any case, rebooting or saving).
- I assume this comes from automatic data binding - although I thought that fields not represented on the form would not overwrite existing model data passed down.
- , . .
:
// POST: /Project/Create/<viewModel>
[Authorize, AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind(Exclude = "Id,id")] ProjectViewModel model)
{
if (model.SubmitValue == "Create")
{
try
{
model.Project.Id = Guid.NewGuid();
model.Save(this.User.Identity.Name);
return this.RedirectToAction("Details", new {id = model.Project.Id});
}
catch (Exception e)
{
this.ModelState.AddModelError(e.ToString(), e.ToString());
}
return View(model);
}
if(model.SubmitValue == "AddTasks")
{
return this.View("AddTasks",model);
}
return this.View(model);
}
//POST: /Project/AddTasks/ + model
[Authorize, AcceptVerbs(HttpVerbs.Post)]
public ActionResult AddTasks([Bind(Include = SelectedCarrierTasks")]ProjectViewModel model)
{
return View(model);
}
: , ?
(TempData) JS- , , .
,