[英]In MVC4, how to redirect to a view from a controller action with parameters in the Url?
[英]How to pass parameters from View to Controller without using Ajax in MVC4?Is it possible?
嗨,是否可以在不使用 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.73\SQLEXPRESS,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");
}
嗨,如果我选择“起始日期”和“截止日期”并单击“确定”按钮,它需要将这两个日期传递给此处的控制器,则我的视图中有两个字段
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
}
此外,由于您已经将 View 绑定到模型,您实际上可以更改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 块中使用此方法。 在这种情况下,该方法会在 using 块的末尾呈现结束</form>
标记。
您可以传递整个模型对象而不是 separatorProperties:
[HttpPost]
public ActionResult GetDates(YourModelClass model)
{
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.