在同一行中使用 2 个提交按钮,在 MVC 中具有不同的功能
Using 2 submit button in a same row with different function in MVC
在我看来,我使用了 2 个提交按钮,搜索和重置
<p>
Name: @Html.TextBox("searcher", (string) ViewBag.searchValue)
<input type="submit" value="Search" /><input type="submit" name="Reset" value="Default" />
</p>
在 Controller 中,我分配给 Default 按钮以清除所有搜索值和参数。
[HttpPost, HttpParamAction]
public ActionResult Reset()
{
ViewBag.searchValue = "";
Index("", "", "");
return View();
}
但是,结果不是清除所有参数和搜索值,默认 按钮现在就像另一个 搜索 按钮一样。我已经在我的控制器中导入了 System.Reflection
一个输入按钮就可以了。
<input type="button"
name="Reset" value="Default"
onclick="location.href='<%: Url.Action("Reset", "Controller") %>'" />
我试试下面的方法,
型号:Product.cs
public class Product
{
public string ID { get; set; }
public string Name { get; set; }
public string Brand { get; set; }
public string Price { get; set; }
}
控制器:HomeController.cs
[HttpPost]
public ActionResult SaveProduct(Product obj)
{
ViewBag.Message = "Product saved successfully!";
return View("Index", obj);
}
[HttpPost]
public ActionResult CancelProduct(Product obj)
{
ViewBag.Message = "The operation was cancelled!";
return View("Index", obj);
}
查看:index.cshtml
@model ViewBag_array.Models.Product
<h1>@ViewBag.Message</h1>
@Html.DisplayForModel()
@using (Html.BeginForm("", "Home"))
{
@Html.EditorForModel()
<br />
<input type="submit" name="save" value="Save" formaction="SaveProduct" formmethod="post" />
<input type="submit" name="cancel" value="Cancel" formaction="CancelProduct" formmethod="post" />
}
通过这种方式,您可以更清楚地分离关注点,并且代码更具可读性。
在我看来,我使用了 2 个提交按钮,搜索和重置
<p>
Name: @Html.TextBox("searcher", (string) ViewBag.searchValue)
<input type="submit" value="Search" /><input type="submit" name="Reset" value="Default" />
</p>
在 Controller 中,我分配给 Default 按钮以清除所有搜索值和参数。
[HttpPost, HttpParamAction]
public ActionResult Reset()
{
ViewBag.searchValue = "";
Index("", "", "");
return View();
}
但是,结果不是清除所有参数和搜索值,默认 按钮现在就像另一个 搜索 按钮一样。我已经在我的控制器中导入了 System.Reflection
一个输入按钮就可以了。
<input type="button"
name="Reset" value="Default"
onclick="location.href='<%: Url.Action("Reset", "Controller") %>'" />
我试试下面的方法,
型号:Product.cs
public class Product
{
public string ID { get; set; }
public string Name { get; set; }
public string Brand { get; set; }
public string Price { get; set; }
}
控制器:HomeController.cs
[HttpPost]
public ActionResult SaveProduct(Product obj)
{
ViewBag.Message = "Product saved successfully!";
return View("Index", obj);
}
[HttpPost]
public ActionResult CancelProduct(Product obj)
{
ViewBag.Message = "The operation was cancelled!";
return View("Index", obj);
}
查看:index.cshtml
@model ViewBag_array.Models.Product
<h1>@ViewBag.Message</h1>
@Html.DisplayForModel()
@using (Html.BeginForm("", "Home"))
{
@Html.EditorForModel()
<br />
<input type="submit" name="save" value="Save" formaction="SaveProduct" formmethod="post" />
<input type="submit" name="cancel" value="Cancel" formaction="CancelProduct" formmethod="post" />
}
通过这种方式,您可以更清楚地分离关注点,并且代码更具可读性。