How do I do an ajax call with jQuery so that I can use $.ajax to post the ViewModel to controller's action method?
I have not used the Form
element because, I don't want postback...
My form looks like this so far:
HTML:
@model comp.learn.data.Models.ProductViewModel
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
<fieldset>
<legend>ProductViewModel</legend>
<div id="CreateDiv">
<div class="editor-label">
@Html.LabelFor(model => model.ProductName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ProductName)
@Html.ValidationMessageFor(model => model.ProductName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Cost)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Cost)
@Html.ValidationMessageFor(model => model.Cost)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Description)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Description)
@Html.ValidationMessageFor(model => model.Description)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ProductTypeId)
</div>
<div class="editor-field">
@Html.DropDownList("ProductTypeId", "Choose item")
@Html.ValidationMessageFor(model => model.ProductTypeId)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ProductTypeName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ProductTypeName)
@Html.ValidationMessageFor(model => model.ProductTypeName)
</div>
</div>
<p>
<input type="submit" value="Create" id="btnSubmit" />
</p>
</fieldset>
<div>
@Html.ActionLink("Back to List", "Index")
</div>
jQuery/JavaScript:
$.ajax(
{
url: '@Url.Action("CreateProduct","ProductManagement")',
dataType: 'json',
contentType: 'application/json; charset=utf-8',
type: 'post',
cache: false,
data: ///what should i write here ,
success: function (data) { alert('final'); },
error: function (f1, f2, f3) { alert(f3); }
});