如何在MVC4中不使用Ajax的情况下将参数从View传递到Controller?有可能吗



嗨,是否可以在不使用ajax的情况下将参数从视图发送到控制器?

我的视图

  @Html.LabelFor(model => model.FromDate)
  @Html.TextBoxFor(model => model.FromDate, new { @class = "form-control", type = "text" })
  @Html.LabelFor(model => model.ToDate)
  @Html.TextBoxFor(model => model.ToDate, new { @class = "form-control", type = "text" })
 <input id="Button1" type="button" value="Ok" />

我的控制器

public ActionResult GetDates(string FromDate, string ToDate)
{
    DateTime fromdt = Convert.ToDateTime(FromDate);
    DateTime todt = Convert.ToDateTime(ToDate);
    SqlConnection con = new SqlConnection(@"Data Source=192.168.0.73SQLEXPRESS,14330;Initial Catalog=WafeERP_NEW;User ID=sa;Password=wafewin;");
    DataTable dt = new DataTable();
    try
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("Select * from View_VisitorsForm where  VisitingDate >='" + fromdt  +"'and VisitingDate <= '" + todt  +"'", con);
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        adp.Fill(dt);
    }
    catch (Exception ex)
    {
        throw;
    }
    ReportClass rc = new ReportClass();
    rc.FileName = Server.MapPath("/Reports/rpt_DailyCustomerVisitSummary.rpt");
    rc.Load();
    rc.SetDataSource(dt);
    Stream stream = rc.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
    return File(stream, "application/pdf");
}

嗨,如果我选择"起始日期"one_answers"截止日期"并单击"确定"按钮,我的视图中有两个字段,它需要将这两个日期传递给这里的控制器

public ActionResult GetDates(string FromDate, string ToDate )

有可能吗?使用ajax是可能的,但我不希望这样。我想在不使用ajax的情况下将这些参数传递给控制器。请任何人告诉我解决办法。

提前感谢。。

设置表单时,您应该能够将参数传递给控制器。

@Using(Html.BeginForm()){
  @Html.LabelFor(model => model.FromDate)
  @Html.TextBoxFor(model => model.FromDate, new { @class = "form-control", type = "text" })
  @Html.LabelFor(model => model.ToDate)
  @Html.TextBoxFor(model => model.ToDate, new { @class = "form-control", type = "text" })
 <input id="Button1" type="button" value="Ok" />
}

此外,由于看起来您正在使用模型,因此可以将模型传递回控制器。还要确保设置[HttpPost]数据注释,表示它用于将数据发布到.

[HttpPost]    
public ActionResult GetDates(YourModel yourModel)
{
    var from = yourModel.FromDate;
    var to = yourModel.ToDate;
}

您应该考虑创建一个<form>来包装您的内容,并使用传统的表单提交将其发布到服务器。这通常是处理此类行为的最常见方式。

因此,您只需要使用指向特定URL的基本<form>元素来包装视图中的内容(在这种情况下,它是使用Url.Action()方法解决的:

<form action='@Url.Action("GetDates","YourController")'>
    @Html.LabelFor(model => model.FromDate)
    @Html.TextBoxFor(model => model.FromDate, new { @class = "form-control", type = "text" })
    @Html.LabelFor(model => model.ToDate)
    @Html.TextBoxFor(model => model.ToDate, new { @class = "form-control", type = "text" })
   <input id="Button1" type="submit" value="Ok" />
</form>

既然您使用的是MVC,那么您也可以考虑使用Html.BeginForm()帮助程序,它基本上也会做同样的事情。

然后只需确保您的控制器操作使用[HttpPost]属性进行装饰,以便它可以实际接受POST请求:

[HttpPost]
public ActionResult GetDates(string FromDate, string ToDate)
{
    // Omitted for brevity
}

此外,由于您已经将视图绑定到模型,因此您实际上可以更改GetDates()方法的参数,以期望模型的实例(将由.NET的模型绑定填充):

[HttpPost]
public ActionResult GetDates(YourModel model)
{
    // Omitted for brevity (you can access your properties using
    // Model.FromDate and Model.ToDate respectively here
}

使用FormExtensions.BeginForm方法之一。在最简单的场景中:

@using (Html.BeginForm("GetDates", "YourController"))
{
    @Html.LabelFor(model => model.FromDate)
    @Html.TextBoxFor(model => model.FromDate, new { @class = "form-control", type = "text" })
    @Html.LabelFor(model => model.ToDate)
    @Html.TextBoxFor(model => model.ToDate, new { @class = "form-control", type = "text" })
    <input type="submit" value="Ok" />
}

BeginForm方法呈现将由控制器操作方法处理的窗体。

可以在using块中使用此方法。在这种情况下,该方法在使用块的末尾呈现关闭的</form>标记。

您可以传递整个模型对象,而不是单独的Properties:

[HttpPost]
public ActionResult GetDates(YourModelClass model)
{
    ...
}

最新更新