View
jquery
function Save()
{
$.ajax
({
url: "@(Url.Action("SaveMember", "Members"))",
data: { name: $("#MemberName").val(),designation: $("#ddlDesignation option:selected").val() },
contentType: "application/json; charset=utf-8",
type: 'GET',
dataType: "json",
success: function (result)
{
alert("Record saved successfully.");
},
error: function ()
{
alert("Something went wrong, please try again");
}
});
}
html
<div class="col-sm-4 tb-font">
@Html.Label("name", "Member Name", new { style = "width:140px; margin-bottom:5px; margin-top:5px; float: left;" })
@Html.TextBox("MemberName", null, new { @class = "form-control text-box", @onchange = "ValidateName(this)" })
</div>
<div class="col-sm-4 tb-font">
@Html.Label("des", "Designation", new { style = "width:140px; margin-bottom:5px; margin-top:5px; float: left;" })
<select id="ddlDesignation" name="ddlDesignation" class="form-control dropdown" style="margin-bottom:2px"></select>
</div>
EXplanation
Members is the name of controller to which data is to be passed.
SaveMember is the name of method in Members controller yo which data is to be passed.
name is is the first parameter passed to SaveMember method in controller.
MemberName is the id of HTML textbox that contains member name and the text in it will be passed to name parameter of SaveMember Method.
designation is the second parameter passed to SaveMember method in controller.
ddlDesignation is the id of HTML dropdownlist that contains designations and the selected designation will be passed to the designation parameter of SaveMember Method. so $(“#ddlDesignation option:selected”).val() means the selected value of ddlDesignation will be passed
type is get and datatype is json
SaveMember in controller will be like this
public JsonResult SaveMember(string name, string designation)
{ ......
}
If the data is saved successfully, it will alert otherwise it will give an error
Leave a comment