在MVC中的同一行中使用2个提交按钮

在我的视图中,我使用了2个提交按钮,即搜索和重置

    <p>
        Name: @Html.TextBox("searcher",(string) ViewBag.searchValue) 
                <input type="submit" value="Search" /><input type="submit" name="Reset" value="Default" />
    </p>

然后在Controller中,我分配给 Default 按钮以清除所有searchValue和Parameter。

        [HttpPost,HttpParamaction]
        public actionResult Reset()
        {
            ViewBag.searchValue = "";
            Index("","","");
            return View();
        }

但是,结果而不是清除所有参数和searchValue,默认按钮现在就像另一个正在搜索按钮一样。我已经在控制器中导入了 System.Reflection

wwl776 回答:在MVC中的同一行中使用2个提交按钮

我尝试以下方法,

型号: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" />
}

通过这种方式,您可以更清晰地分离关注点,并使代码更具可读性。

,

输入按钮可以解决问题。

<input type="button"
         name="Reset" value="Default"
         onclick="location.href='<%: Url.Action("Reset","Controller") %>'" />
本文链接:https://www.f2er.com/2634126.html

大家都在问