开发者

View Called from Partial Not Submitting Form Data

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.


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) ){ %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜