View(normal view)
@model HoleObject
<div id="add">
@Html.Partial("_add", Model.Add)//add
</div>
<div id="list">
@Html.Partial("_list", Model.ListObj)//display the list
</div>
<script>
$("#Add").on("submit", "form", function (e) {
$(".js-loading").removeClass("hide");
e.preventDefault();
var form =new FormData($(this)[0])
// var form = $(this);
$.ajax({
url: 'localhost/Add',
type: "POST",
data: form.serialize(),
success: function (response) {
if (response.Errors != null && response.Errors != '') {
alert(response.Errors)
}
else {
getList ();
}
},
error: function (errorcode, textStatus, errorThrown) {
alert("Error '" + errorcode.status + "' (textStatus: '" + textStatus + "', errorThrown: '" + errorThrown + "')");
},
complete: function () {
$(".js-loading").addClass("hide");
}
});
});
var getList = function () {
$.ajax({
url: 'localhost/List',
data: { id: '@Model.Id' },
type: "GET",
success: function (html) {
if ($.trim(html) != '') {
$("#list").html('');
$("#list").html(html);
$(".js-loading").removeClass('hide');
}
}
});
}
getList ();
<script>
-------------------------------------------------------------------------------------------------------------------------
//controller
public PartialViewResult Add(int id)
{
var model = new Add();
try
{
if(id!=0)
model=ExistData where id==id//get existing data for edit
}
catch (Exception ex)
{
ex.LogException();
}
return PartialView("_add", model);
}
[HttpPost]
public JsonResult Add(Add model)
{
if (!ModelState.IsValid)
return AsError();
try
{
//insert into db
}
catch (Exception ex)
{
ex.LogException();
}
return Json("success")
}
public PartialViewResult List()
{
var model = new List<ListObj>();
try
{
model =getlist from db.ToList();
}
catch (Exception ex)
{
ex.LogException();
}
return PartialView("_list", model);
}