【问题标题】:mvc passing parameter to controllermvc 将参数传递给控制器
【发布时间】:2013-08-19 08:53:26
【问题描述】:

我对 MVC 很陌生

我需要一些帮助来解决表单提交时将参数传递给控制器​​的问题

我得到的是以下控制器和视图

public ActionResult Index(string method ="None")
    {
        if (Request.HttpMethod == "POST")
        {

            switch (method)
            {
                case "Add10":
                    _bag.GetBag = Get100Products().Take(10).ToList<Product>();
                    break;
                case "Clear":
                    _bag = null;
                    _bag.GetBag = null;
                    _bag = new Models.Bag();
                    break;
                case "Add":
                    if ((Request.Form["Id"] != null) && (Request.Form["Id"] != ""))
                    {
                        if (_bag.GetBag.Count < 100)
                        {
                            var p = GetProduct(Request.Form["Id"]);
                            int qnt = Convert.ToInt16(Request.Form["qnt"]);
                            if (p.ItemNumber != null)
                            {
                                p.Quantity = qnt;
                                p.Index++;
                                _bag.Item = p;
                            }
                        }
                    }
                    break;

            }
        }
  return View(_bag.GetBag);
 }

和视图的视图部分

 <div style="vertical-align:middle">

@using (Html.BeginForm("", "Home", new { method = "Add10" }, FormMethod.Post))
{
<!-- form goes here -->

 <input type="submit" value="Add 10 Items to bag" />

}

 @using (Html.BeginForm("GetDiscount", "Home", FormMethod.Post))
{
 <div>
 <!-- form goes here -->

  <input type="submit" value="Get Discount" />
    With MAX time in seconds  <input type="text" name="time" maxlength="2" value="2" />

  </div>
}


@using (Html.BeginForm("", "Home", new { method = "Clear" }, FormMethod.Post))
 {
   <input type="submit" value="Empty the bag" />
 }
</div> 

所以我期待当使用点击按钮 Add 10 Items to bag 以将方法值“Add10”传递给索引控制器时,当点击清空包以传递“Clear”索引控制器中的方法值时

但它总是显示为“无”

我做错了什么?

</form>

【问题讨论】:

    标签: asp.net-mvc-4


    【解决方案1】:

    首先,您必须将[HttpPost] 添加到您的控制器才能接受 POST 请求:

    [HttpPost]
    public ActionResult Index(string method ="None")
        {
    

    【讨论】:

    • 是的,我已将控制器更改为接受一个用于 [HttpGet] 和一个用于 [HttpPost] 但问题仍然存在
    【解决方案2】:

    您应该区分 GET 和 POST 操作。

    你可以这样做:

    // [HttpGet] by default
    public ActionResult Index(Bag bag = null)
    {
       // "bag" is by default null, it only has a value when called from IndexPOST action.
       return View(bag);
    }
    
    [HttpPost]
    public ActionResult Index(string method)
    {
       // Your logic as specified in your question
    
       return Index(_bag.GetBag);
    }
    

    编辑:

    您的代码有误,例如您将得到一个NullReferenceException,因为您尝试调用空对象 (_bag) 上的属性:

    _bag = null;
    _bag.GetBag = null; // NullReferenceException: _bag is null!
    

    此外,如果我们将 Action 拆分为多个操作并遵循技术理念,您的代码会更简洁、更易于维护。

    您是否考虑将这段代码重构为更小、更易于理解的块?

    【讨论】:

    • 是的,我已经按照您的建议进行了更改,但仍然存在原始问题
    • 看看我的编辑,我认为最好的方法是重构你的代码并修复现有的问题。你的代码会更干净,更易于维护。在此之后,我认为您的问题将被自动修复。
    猜你喜欢
    • 2010-09-14
    • 1970-01-01
    • 2022-10-31
    • 2012-12-18
    • 2016-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-14
    相关资源
    最近更新 更多