I want to upload partial view data without reloading the html. i have used Json to get the data but I think there is some issues in Script. The Success part is not executing.
[HttpPost]
public JsonResult HorseTracker(ClsHorseTracker model)
{
try
{
if (ModelState.IsValid)
{
horseTrackerDetails.InsertUpdateHorseTracker(model);
}
}
return Json(model, JsonRequestBehavior.AllowGet);
}
catch
{
return Json(new { success = false });
}
}
[ChildActionOnly]
public PartialViewResult HorseTrackerDetails()
{
return PartialView("_pHorseTrackerDetails", horseTrackerDetails.HorseTrackerList());
}
Main View
@using (Html.BeginForm("HorseTracker", "HorseTracker", FormMethod.Post, new { id = "CreateForm" }))
{
<div class="panel panel-default" style="font-size:12px;">
<div class="panel-body">
<div class="form-group">
@Html.TextBoxFor(m => m.HorseName)
@Html.DropDownListFor(m => m.HorseTypeName, Model.HorseTypeList)
</div>
<input type="submit" value="Save" class="btn btn-primary pull-right" />
</div>
</div>
}
</div>
<div id="partial" class="col-md-8">
@Html.Action("HorseTrackerDetails", "HorseTracker")
</div>
Partial View
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.HorseName)
</th>
<th>
@Html.DisplayName("Type")
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@item.HorseName
</td>
<td>
@item.HorseTypeName
</td>
</tr>
</table>
Script
$(document).ready(function () {
var url = '@Url.Action("HorseTracker", "HorseTracker")';
$('#CreateForm').submit(function () {
if (!$(this).valid()) {
return;
}
$.post(url,$(this).serialize(), function (response) {
if (response.success) {
debugger;
$('#partial').html(response);
}
else {
var message = response.message;
alert(message);
}
});
return false;
})
})
model(ClsHorseTracker) but not includessuccessresponse, henceif (response.success)always returns false. You need to add something likesuccess = trueinreturn Jsonpart.$('#partial').html(response);executes but still do not see changes in the form unless I refresh the page.responsereturns proper HTML partial page response. If it returns something other than HTML markups, you should know why the response is not rendered properly.html, it doesnot show markup. But response contains data inside. Where I am doing wrong.Please guide me.return PartialViewinstead ofreturn Jsonon successful result with viewmodel - you only need to check if the request fails withsuccess = falseexist as response.