从视图调用控制器,传递输入文本字段

发布于 2024-12-05 17:32:05 字数 419 浏览 2 评论 0原文

我有两个文本输入,当单击按钮时,需要将其内容作为参数传递给某个操作。我正在使用MVC3 视图:

<input name="input2" type="text"  class="inputfield" id="datepicker_1" /></td>
<input name="input2" type="text"  class="inputfield" id="datepicker_2" /></td>

@Html.Action("Search", ...)

控制器: 公共 ActionResult 搜索(...) 我想为此应该在@Html.Action 中使用对象routeValues 或RouteValueDictionary。这些对象让我有点困惑。有人可以帮我澄清一下吗?谢谢你!

I have two text inputs, content of which needs to be passed to a certain action as parameters when a button is clicked. I'm using MVC3
View:

<input name="input2" type="text"  class="inputfield" id="datepicker_1" /></td>
<input name="input2" type="text"  class="inputfield" id="datepicker_2" /></td>

@Html.Action("Search", ...)

Controller:
public ActionResult Search(...)
I suppose the object routeValues or RouteValueDictionary should be used in the @Html.Action for this. These object are confusing for me a bit. Could anyone clarify this for me please. Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

童话 2024-12-12 17:32:05

Html.Action 可能会在您提供输入之前生成链接 html。您需要将输入放在要提交给您的操作的表单中,或者使用 ajax(可能带有 jquery)来调用该操作,如下所示:

@using (Html.BeginForm("Search", "Controller", FormMethod.Post, new { id = "frmAction" }))
{
    <input name="datepicker_1" type="text"  class="inputfield" id="datepicker_1" /></td>
    <input name="datepicker_2" type="text"  class="inputfield" id="datepicker_2" /></td>
}


[HttpPost]
public ActionResult Search(Datetime datepicker_1, Datetime datepicker_2) {...}

对于 Ajax 示例,请检查此问题:

适用于 ASP.NET MVC 3 的 jquery ajax 表单

希望这会有所帮助...

The Html.Action will probably generate the link html before you provide the inputs. You need to either place your inputs inside a form to be submited to your action, or use ajax, with jquery perhaps, to call the action, like so:

@using (Html.BeginForm("Search", "Controller", FormMethod.Post, new { id = "frmAction" }))
{
    <input name="datepicker_1" type="text"  class="inputfield" id="datepicker_1" /></td>
    <input name="datepicker_2" type="text"  class="inputfield" id="datepicker_2" /></td>
}


[HttpPost]
public ActionResult Search(Datetime datepicker_1, Datetime datepicker_2) {...}

For an Ajax example, check this question:

jquery ajax forms for ASP.NET MVC 3

Hope this helps...

攒一口袋星星 2024-12-12 17:32:05

似乎您需要在视图上进行提交,然后在控制器上实施后操作来处理提交。即视图中的某些内容

 <input type="submit" value="Search..." />

[HttpPost]
public ActionResult Search(FormCollection collection)

参数取决于您的视图是否是强类型的。以上假设不是。

It seems like you need have a submit on your view, and then implement a post action on your controller, to handle the submit. I.e. something along the lines of

 <input type="submit" value="Search..." />

in the view, and

[HttpPost]
public ActionResult Search(FormCollection collection)

the parameters depend on whether your view is strongly typed. The above assumes its not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文