[英]JQuery MVC3 How do I pass a parameter from a button submit and get the action result back
I have a form that has a combo box and a div underneath it. 我有一个窗体,在其下面有一个组合框和一个div。 It has show and delete buttons next to combo box. 组合框旁边有显示和删除按钮。
When someone selects an item in the combo box and then click on the Show button, I want the button to call an action in the controller and pass the selected value of combo box to the action. 当某人在组合框中选择一个项目,然后单击“显示”按钮时,我希望该按钮在控制器中调用一个动作并将该组合框的选定值传递给该动作。 I then want it to load the view in the viewDiv div. 然后,我希望它在viewDiv div中加载视图。
For example: 例如:
<input type="submit" value="Show" onclick="alert($('#SelectedId').val(););" />
<div id="viewDiv"/>
I want onclick to call a action in controller and pass the Id = $('#SelectedId').val(); 我想onclick在控制器中调用一个动作并传递Id = $('#SelectedId')。val(); and then populate the viewDiv with the contents of the view. 然后用视图的内容填充viewDiv。
Please guide as I am very stuck 请指导,因为我很困
Try this. 尝试这个。 Assuming your submit button has an id = "btn" this should work: 假设您的提交按钮的id =“ btn”,则应该可以使用:
$("#btn").bind("click", function(e){
e.preventDefault();
$("#viewDiv").load("yoururl", { id = $("#SelectedId").val() });
});
There are a lot of different ways you can accomplish this. 您可以通过许多不同的方法来完成此任务。
I would recommend wrapping all of your fields in a form. 我建议您将所有字段包装成表格。 Then if you submit the form, your action can be called and any data on the form can be returned. 然后,如果您提交表单,则可以调用您的操作,并可以返回表单上的任何数据。
@using (Html.BeginForm("Login", "Account")
In MVC talk, you can use something like this to call a Login action on an Account controller. 在MVC对话中,您可以使用类似这样的方法在Account控制器上调用Login操作。 This is the equivalent to 这相当于
<form action="/Account/Login">
</form>
Now you can submit this form with jQuery or with a input type=submit button, etc. 现在,您可以使用jQuery或输入type = submit按钮等方式提交此表单。
The key to getting your variable to the action function would be to just name the combo box id the exact same name as the variable. 将变量添加到动作函数的关键是仅将组合框ID命名为与变量完全相同的名称。
You can also return the entire model that's on the page by having the model's class as the class you are expecting 您还可以通过将模型的类作为您期望的类来返回页面上的整个模型
public ActionResult Login(LoginModel model);
You can also pass in custom variables: 您还可以传递自定义变量:
@using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }
and 和
public ActionResult Login(string returnUrl);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.