简体   繁体   English

jQuery MVC3如何从按钮提交传递参数并返回操作结果

[英]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.

相关问题 如何将值从View传递到Controller Action参数? MVC3 - How to pass values from View to a Controller Action parameter? MVC3 如何创建带有提交按钮的简单搜索框,以带回MVC中的结果集? - How do I create a simple search box with a submit button to bring back a result set in MVC? 如何使用Jquery Ajax将参数传递给mvc ActionResult? - How do I pass a parameter to an mvc ActionResult using Jquery Ajax? 在 MVC3 中,如何获取发布到我的操作的 DataContract 属性对象? - In MVC3 how do I get a DataContract-attributed object that was posted to my action? 当呈现的操作链接作为查询字符串返回时,如何解决MVC3的路由问题? - How do I fix a routing issue with MVC3 when rendered action links get returned as querystrings? 使用 jQuery 将参数从 MVC 视图传递到操作 - Pass parameter from MVC view to an action using jQuery 我如何在MVC3 jquery应用程序中实现长轮询 - How do I implement long polling in MVC3 jquery application 如何将MVC操作参数重新映射到另一个参数名称? - How do I remap an MVC action parameter to another parameter name? 如何获取剃须刀中单选按钮的值以将其作为参数传递给操作? - How to get the value of a radio button in razor to pass it as a parameter to an action? 在提交时,操作不会转到MVC3中的控制器 - On Submit, action not going to controller in MVC3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM