asp.net mvc明确设置动作

时间:2010-02-07 06:38:50

标签: asp.net asp.net-mvc

我的应用程序中有2个输入操作视图。

第一个视图(让我们称之为view1)提交一个表单。根据表单完成对数据库的一些操作,并返回第二个视图(View2),其中包含来自数据库的一些其他数据作为模型。

控制器动作代码:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult View1(FormCollection fc)
{
   //database ops to fill vm properties    
   View2ViewModel vm=new View2ViewModel();

   return View("View2", vm);
}

现在,由于我返回一个新视图而不是动作重定向,因此网址仍为http://www.url.com/View1,但一切都按预期工作。

问题:

当我在View2中提交表单时,它调用View1操作方法,而不是View2操作方法。可能是因为网址仍然是View1。

如何调用动作View2

2 个答案:

答案 0 :(得分:1)

您的控制器方法应如下所示:

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult View1(int id)
{
   //database ops to fill vm properties    
   View1ViewModel vm=new View1ViewModel(id);

   return View("View1", vm);
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult View1(FormCollection fc)
{
   // Code here to do something with FormCollection, and get id for
   // View2 database lookup

   return RedirectToAction("View2", id);
}

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult View2(int id)
{
   // Do something with FormCollection 

   return View("View2", vm);
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult View2(int id)
{
   // Do something with FormCollection 

   return RedirectToAction("View3", id);
}

......等等。

答案 1 :(得分:0)

您可以在任何您想要的视图中,并将表单提交给您的应用程序中的任何控制器操作,因为您可以指定

<% using (Html.BeginForm("ThAction", "Controller"))
   { %>

   Enter your name: <%= Html.TextBox("name") %>
   <input type="submit" value="Submit" />

<% } %>