views:

37

answers:

1

Hi! I hope I am able to put this question together well. In a partial view I have a link to a create action:

public ActionResult CreateProject()
{
    return View("EditProject", new Project());
}

Now this loads another view which allows editing of the blank model passed to it. But when form is submitted it is supposed to post to:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditProject(Project record)
{
    if (ModelState.IsValid)
    {
        projectRepo.saveProject(record);
        return View("Close");
    }
    else
    {
        return View("EditProject");
    }
}

This method works for many of the tables and edit actions work just as well for the same view. But only for the create action (with the blank model) the form keeps calling to the create action, as I traced with the debugger.

One of my team mates has solved this problem so:

[AcceptVerbs(HttpVerbs.Get)]
public ViewResult EditProject(int id)
{
    Project project = null;
    if (id == 0)
    {
        project = new Project();
    }
    else
    {
        project = (from p in projectRepo.Projects
                   where p.ProjectID == id
                   select p).First();
    }

    return View(project);
}

And in the partial instead of having <%= Html.ActionLink("Create New", "CreateProject")%> there'd be <%= Html.ActionLink("Create New", "CreateProject", new { id = 0 })%>.

Now I was hoping to find out why the previous method would not go through, since it does for other tables in other views. Thanks.

A: 

By default your form will post to same URL it was rendered at. Since you called create action it will post back to create action, and not edit, 'cos views do not matter (-:

Explicitly use

 <%= using( Html.BeginForm("Action","Controller) ){ %>
Alexander Taran
Thanks a lot! :D I have got to get my basics polished it seems. Thanks again :)
aredkid