【问题标题】:asp.net mvc set action explicitlyasp.net mvc 明确设置操作
【发布时间】:2010-02-07 06:38:50
【问题描述】:

我的应用程序中的输入操作有 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);
}

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

问题:

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

我该怎么做才能调用动作 View2

【问题讨论】:

标签: asp.net asp.net-mvc


【解决方案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);
}

...等等。

【讨论】:

    【解决方案2】:

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

    <% using (Html.BeginForm("ThAction", "Controller"))
       { %>
    
       Enter your name: <%= Html.TextBox("name") %>
       <input type="submit" value="Submit" />
    
    <% } %>
    

    【讨论】:

      猜你喜欢
      • 2011-02-18
      • 1970-01-01
      • 1970-01-01
      • 2010-11-29
      • 1970-01-01
      • 1970-01-01
      • 2018-12-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多