【问题标题】:@Html.BeginForm does not redirect to assigned ActionName@Html.BeginForm 不会重定向到分配的 ActionName
【发布时间】:2018-09-21 12:06:41
【问题描述】:
@using (Html.BeginForm("SerialNumber", "Test", FormMethod.Get))
   {
                    <div class="form-group">
                        <label class="control-label col-md-8 col-sm-3 col-xs-12" for="color-name">
                            Serial Number <span class="required">*</span>
                        </label>
                        <div class="col-md-4 col-sm-6 col-xs-12 input-group">
                            @Html.TextBox("serial_number", null, new { @id = "txtSearch", @class = "form-control col-md-7 col-xs-12", type = "text", required = "required" })
                            <span class="btn btn-default input-group-addon">
                                <span class="glyphicon glyphicon-barcode"></span>
                            </span>
                        </div>
                        <input type="submit" value="Search" class="btn btn-primary" />
                    </div>
 }

我正在尝试将 TextBox“serial_number”值传递给测试控制器的“SerialNumber”ActionResult。但该值仅传递给测试控制器的“AddSerialNumber”ActionResult。我已经尽力了,但我找不到解决方案。请帮忙。

Please See the Application ScreenShot.

    public ActionResult AddSerialNumber()
    {
        return View(db.sales.ToList()); 
    }

    [HttpGet]
    public ActionResult SerialNumber(int serial_number)
    {
         ...
         return View();
    }

【问题讨论】:

  • 我假设你已经用[HttpPost] 属性装饰了SerialNumber
  • 你的 ruteMap 有什么奇怪的地方吗?
  • ActionResult SerialNumber 中是否还有其他代码(...)?
  • 你的表单有一个FormMethod.Get?!!
  • 是的,还有其他代码可以从该序列号添加数据。 @RickL

标签: asp.net asp.net-mvc asp.net-mvc-4 razor


【解决方案1】:
[HttpGet]
public ActionResult SerialNumber(string serial_number)
{
     ...
     return View();
}

你必须使用字符串变量来获取文本框的值。

【讨论】:

  • 感谢您的回答,但这里不是这样。
  • 我认为使用 HttpGet 无法将数据发布到控制器操作。
【解决方案2】:

尝试以下方法。

创建您的视图模型,也许还有一个类来保持销售。

public class ViewModel
{
    public List<Sale> Sales { get; set; }
    public string SerialNumber { get; set; }
}

public class Sale
{
    public string Field1 { get; set; }
    public string Field2 { get; set; }
}

然后在您的控制器中,执行以下操作。

  [HttpGet]
  public ActionResult AddSerialNumber()
  {
      var myModel = new ViewModel();
      myModel.Sales = db.sales.ToList();

      return View(myModel);
  }

  [HttpPost]
  public ActionResult SerialNumber(ViewModel modelPosted)
  {
        //Do stuf with model
        return View();
  }

然后从模型中构造你的视图并更改为 Post 指令。

@model ViewModel
@using (Html.BeginForm("SerialNumber", "Test", FormMethod.Post))
        {
            <div class="form-group">
                <label class="control-label col-md-8 col-sm-3 col-xs-12" for="color-name">
                    Serial Number <span class="required">*</span>
                </label>
                <div class="col-md-4 col-sm-6 col-xs-12 input-group">
                    @Html.TextBoxFor(x=> x.SerialNumber, new { @class = "form-control col-md-7 col-xs-12", required = "required" })
                    <span class="btn btn-default input-group-addon">
                        <span class="glyphicon glyphicon-barcode"></span>
                    </span>
                </div>
                <input type="submit" value="Search" class="btn btn-primary" />
            </div>
        }

然后这将发布到您的控制器。我在本地进行了简单的设置,并且可以正常工作。

希望对您有所帮助。

附言。得走了..祝你好运

【讨论】:

    【解决方案3】:

    尝试使用formmethod POST 和注释POSTcontroller action 方法发布数据。

    并为您提供为txtSearch的文本框提供ID为serial_number

    @using (Html.BeginForm("SerialNumber", "Test", FormMethod.POST))
           {
                            <div class="form-group">
                                <label class="control-label col-md-8 col-sm-3 col-xs-12" for="color-name">
                                    Serial Number <span class="required">*</span>
                                </label>
                                <div class="col-md-4 col-sm-6 col-xs-12 input-group">
                                    @Html.TextBox("serial_number", null, new { @id = "serial_number", @class = "form-control col-md-7 col-xs-12", type = "text", required = "required" })
                                    <span class="btn btn-default input-group-addon">
                                        <span class="glyphicon glyphicon-barcode"></span>
                                    </span>
                                </div>
                                <input type="submit" value="Search" class="btn btn-primary" />
                            </div>
         }
     [HttpPost]
        public ActionResult SerialNumber(string serial_number)
        {
             ...
             return View();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-28
      • 1970-01-01
      • 1970-01-01
      • 2012-06-21
      相关资源
      最近更新 更多