使用ajax将FormCollection传递给控制器

时间:2014-01-25 01:00:22

标签: ajax asp.net-mvc forms

如何在下拉列表更改时使用jquery或ajax将表单集合传递给控制器​​?

如果我不使用提交按钮,我可以在我的控制器中调用一个动作并将其提交给我吗?

@using (Html.BeginForm("Create","Order", FormMethod.Post, new {@class = "form-horizontal", @name="forma"})) {
                    @Html.ValidationSummary(true)

                    @Html.Partial("_CreateOrEdit", Model)


                    <div class="form-actions no-margin-bottom">
                        <input type="submit" value=@Html.LocalizeString("String_Save") class="btn btn-primary" >
                          <a href="@Url.Action("Index", "Order")" class="btn btn-primary">@Html.LocalizeString("String_BackToList")</a>
                    </div>
                }

部分是(它有更多的字段,但我认为它们对问题无关紧要)

<div class="control-group">
<div class="control-label">
      @Html.LocalizedLabelFor(model => model.BuyerId)
</div>

<div class="controls">
 @Html.DropDownListFor(model => model.BuyerId, ((IEnumerable<SalesAgent.Core.Entities.Buyer>)ViewBag.PossibleBuyers).Select(option => new SelectListItem 
{
    Text = (option == null ? "None" : option.Name), 
    Value = option.Id.ToString(),
    Selected = (Model != null) && (option.Id == Model.BuyerId)
}), @Html.LocalizeString("String_Choose"),new {@class="searchable" })
@Html.ValidationMessageFor(model => model.BuyerId)

当我更改下拉列表时,我想从我的控制器调用一个动作并将其传递给我当前的表单数据。我该怎么做?

2 个答案:

答案 0 :(得分:1)

    var input = $(':input');
    $.ajax({
        type: "POST",
        data: input,
        url: "URL",
        success: function (items) {
            //TODO
        }
    });

在控制器中,我将它转到FormCollection,然后我可以用表单数据做我需要的。

答案 1 :(得分:0)

我想是这样......

$("#BuyerId").change(function() {
var model = {
    // name of model member : value of model member
    // BuyerId : $("#BuyerId").val(),
    //  ....your form data :)))
}
$.ajax({
    url : //your method of controller url,
    type: "POST",
    data: {model : model} // name of model object what send to your controller
});

});