【问题标题】:concatenate a string from multiple input textboxes连接来自多个输入文本框的字符串
【发布时间】:2013-02-05 11:33:24
【问题描述】:

我有一个 cshtml 页面,我要求用户提供一些输入数据,然后我需要将这些数据连接成一个字符串,以便在我的控制器中构建一个动态 LINQ 查询。此视图不使用模型。到目前为止,这是我的 html 代码。

<div id="filter">
Enter a customer name. It can be a part of a name to get broader results. (optional)
<br />
<input type="text", id="customer", value="") />
<br />
Enter a case status ( OPEN or CLOSED ), or leave blank to get both. (optional)
<br />
<input type="text", id="status", value="") />
<br />
Enter a date range to filter by date. (optional)
<br />
Start Date 
<input type="text", id="startdate", value="") />
End Date
<input type="text", id="enddate", value="") />
<br />
Enter a PromoID (optional)
<br />
<input type="text", id="promoid", value="") />
<br />
Enter a Complaint Code (optional)
<br />
<input type="text", id="complaintcode", value="") />
</div>

@Html.ActionLink("Export Case Data To Excel for Analysis", "CaseReport", "Reports",   "Excel", new { stringFilter = mystring })

控制器动作有一个名为 stringFilter 的字符串参数。 我基本上需要构建一个字符串过滤器并将其传递给控制器​​。我正在使用动态 Linq 查询库。

如何从 DOM 中获取字符串值?

【问题讨论】:

  • 不知道您的问题.. 您需要什么? request.Form 给你什么?你需要文本框的值还是什么?
  • 可能重复? How to retreive form values from HTTPPOST(之后您只需以正常方式连接字符串:"foo" + "bar"
  • @gaurav 我基本上只需要从文本框值构建一个字符串,我可以将其发送到 ActionLink 中的控制器操作。我收集的文本框应该用表格包裹起来吗?我什么时候使用 request.Form?
  • 我在原始问题中添加了此视图不使用模型。许多示例似乎都假设正在使用模型。

标签: asp.net asp.net-mvc-3 linq razor


【解决方案1】:

您可以做的一件事是在按钮单击事件处理程序中将它们全部连接起来,类似于..

$('#form-input-submit-button').click(function() { /* do it here & then submit. */ });

但我建议您在 MVC 控制器操作方法中包含您需要的所有参数

[HttpPost]
public void CaseReport(string promoId, string coplaintCode, ... ) { }

或者最好有强类型模型

public class ReportModel
{
    public string PromoId { get; set; }
    public string ComplaintCode { get; set; }
    ...
}

所以你可以:

[HttpPost]
public void CaseReport(ReportModel model) { /* Validate ModelState */ }

其实,MVC缩写中的model就是你所需要的。

但你也可以这样做

[HttpPost]
public void CaseReport(FormCollection form)
{
}

查看所有传入的数据。

【讨论】:

  • 所以如果我创建一个视图模型,我可以将文本框与 Razor 绑定,然后将该模型传递回控制器操作?我真的不需要具有该方法的表单对象吗?
  • @Ryan 对。只需 &lt;input&gt; 元素,其名称对应于视图模型的属性名称。
猜你喜欢
  • 2013-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-17
  • 1970-01-01
  • 2015-02-09
相关资源
最近更新 更多