简体   繁体   English

使用MVC3和Unobtrusive AJAX,将参数从控件传递到se

[英]Using MVC3 and Unobtrusive AJAX, passing parameters from controls to the se

I have a page, it has a drop down list, which contains the names of objects to edit. 我有一个页面,它具有一个下拉列表,其中包含要编辑的对象的名称。 I have this working so that when you select an item, an AJAX call loads the text and it shows in a textarea. 我的工作原理是,当您选择一个项目时,AJAX调用会加载文本并在文本区域中显示。 Now I want to do 'save', 'save as' and 'delete' buttons, which would make their own AJAX calls to do the obvious. 现在,我想执行“保存”,“另存为”和“删除”按钮,这将使他们自己的AJAX调用变得显而易见。 The Save As would need to use jquery to show a popup to ask for the name to save as. 另存为将需要使用jquery显示一个弹出窗口,要求输入要另存为的名称。 In each case, I want to pass data that is in elements outside the form, to the server. 在每种情况下,我都希望将表单外部元素中的数据传递给服务器。 I can't have all three buttons in different forms, all contain the text area to pass the text in, and I need the selected value from the drop list, which is already it's own form, outside the form that contains the text area. 我不能将所有三个按钮都设置为不同的形式,都包含用于传递文本的文本区域,并且我需要从下拉列表中选择的值,该列表已经是它自己的表单,位于包含文本区域的表单之外。 I can see how I could use one AJAX call and pass all sorts of data that needs parsing to work out what I should do with it, but that seems ugly to me. 我可以看到如何使用一个AJAX调用并传递需要解析的各种数据,以计算出我应该如何处理它,但这对我来说似乎很难。 Is there a neat way, perhaps using JSON ( not not necessarily ), that I can pass the values of controls on my page, but outside my form, in to an AJAX call ? 是否有一种巧妙的方法,也许使用JSON(不一定是JSON),我可以将页面上控件的值(但是在窗体之外)传递给AJAX调用?

So for example, I have this: 因此,例如,我有这个:

    @using (Ajax.BeginForm(new AjaxOptions 
   {
       Url = Url.Action("GetCoupon"),
       OnSuccess = "OnLoadSuccess"
    }))
    {
           @Html.DropDownList("ddlCoupons", 
          Model.Coupons.Select(a => new SelectListItem { Text = a.Name, Value = a.CouponId.ToString() }), 
          "--Empty--",new{
          onchange = "OnSelectCoupon();"
    });
       <input type="submit" value="Load" id="sbmtCoupon" style="visibility:hidden"/>
   }

now, this works. 现在,这有效。 But then I have another AJAX form: 但是然后我有另一个AJAX形式:

@using (Ajax.BeginForm(new AjaxOptions
 {
     Url = Url.Action("SaveCoupon")
 }))
{
    <input type="submit" value="Save" />
}

I want to pass the selected id of the drop down list, and the text of a text area, to this AJAX call. 我想将下拉列表的选定ID和文本区域的文本传递给此AJAX调用。 Right now, I am adding some hidden fields and handling change events to fill the fields in each form ( there's three actions ), so they send what I need, but I'd like to just have some javascript run to build the value passed in, by searching for values across the form. 现在,我要添加一些隐藏字段并处理更改事件以填充每个表单中的字段(有三个操作),以便它们发送我需要的内容,但是我只想运行一些JavaScript来构建传入的值,通过搜索表单中的值。

cant say that i have understood your question but it seems like you need to prevent the default posting of from you can try something like 不能说我已经理解了您的问题,但是似乎您需要阻止默认的发布,您可以尝试执行以下操作

i assume these are the only botton on the page, so hook them up to the click event 我认为这些是页面上唯一的botton,因此将其连接到click事件

$("button").click(function(e){
e.preventDefault(); //prevent the default behaviour of the form 
var buttonpPressed = $(this).data("val");  //get which button is pressed
var valueOfField = $("#idOfyourDDL").val();
$.post("/Home/Index",{buttonPressed:buttonPressed,value:valueOfField},function(data){
//this is the callback which will execute upon successful ajax post
alert(data.actionPerformed);
},'josn');
});

[HttpPost]
the ActionResult would look like

public ActionResult(string buttonPressed){

if(buttonPressed=="saveas"){
// perform some action
return Json(new{actionPerformed="saveas"});
}
else{
//perform someother action
return Json(new{actionPerformed="save"});
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM