@ Html.BeginForm不会重定向到指定的ActionName

时间:2018-04-11 14:34:34

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

@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();
    }

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

尝试以下方法。

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

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>
        }

然后会发布到您的控制器。我在当地进行了简单的设置并且有效。

希望有所帮助。

聚苯乙烯。必须去...祝你好运

答案 2 :(得分:-2)

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

并为serial_number

提供的文本框提供txtSearch的ID
@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();
    }