0

How can I pass form collection to controller with jquery or ajax on change for drop down list?

Can I call an action in my controller and give it my form if I don't use submit button?

@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" [email protected]("String_Save") class="btn btn-primary" >
                          <a href="@Url.Action("Index", "Order")" class="btn btn-primary">@Html.LocalizeString("String_BackToList")</a>
                    </div>
                }

and part of that partial is (it has many more fields but I think they don't matter for question)

<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)

when I change dropdown list I want to call an action from my controller and pass it my current form data. How do I do that?

2
  • Provide some code please Commented Jan 25, 2014 at 1:08
  • That should be enough code :) Commented Jan 25, 2014 at 1:20

2 Answers 2

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

In controller I recive it to a FormCollection and then I can do what I need with form data.

Sign up to request clarification or add additional context in comments.

Comments

0

I think like this...

$("#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
});

});

2 Comments

And how do I catch it in controller? What type is "model" ?
You can "catch" it in parameter of your controller's method. Model in js script is js object, what automatically serilize in object of your Model in controller's method by posting js model in it...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.