Stian
Stian

Reputation: 1299

Need access to model on jquery HTTP (Ajax) request

I want to have access to the model with data from the form when i do an http ajax request.I am not sure if this is possible, so I might have to include the data I need in my request?

From the view I have a jquery.ajax call

$.ajax({
   url: this.href,
   cache: false,
   success: function (html) { $("#editorPriceRows").append(html); }
});

And in my controller I have the action

public ActionResult NewDateRow(MyModel model)
{
   // here I want to check some of the values i filled out in my form but have not posted yet and depending on this value, I will set some data in the model I am passing to the partial view.       
    return PartialView("_DateEditRow", partialModel);
} 

I need to check a date in my form to see what data I need to set inn the model that is passed to the partialview. Is this possible?

Upvotes: 0

Views: 548

Answers (2)

praveen
praveen

Reputation: 1

Yes you can do this using @Ajax.actionlink ex:

@Ajax.ActionLink("Any friedly name", "YourAction", new { uaid = Model.xyzclass.Parametername}, new AjaxOptions { UpdateTargetId = "whateverurplacholder forholdingmsg",OnSuccess = "xyz", OnFailure = "xyz" } }) 

this will work,but since this ajax call, you may not be able to see view source of this once it is executed

$ajaxrequest(....) you can check the syntax.

you could still do that jquery

thanks

Upvotes: 0

JoJa
JoJa

Reputation: 610

You can't access your model directly from jQuery but you can try the following line:

 var model = @Html.Raw(Json.Encode(Model));

Then you have to pass the data to your controller action (cache: false, data:...,).

Upvotes: 1

Related Questions