In my MVC project I tried to render partial views with ajax but I'm not able to create the Routing in it. how can one implement routing in an ajax based partial view.
public ActionResult ViewResult(string view, object viewModel)
{
if (Request.IsAjaxRequest())
{
return PartialView(view, viewModel);
}
return View(view, viewModel);
}
My View
<div class="row">
<div class="x_panel">
<div class="x_title">
Category
</div>
<div class="x_content">
<form method="post" id="frmAddEditCategory" data-parsley-validate class="form-horizontal form-label-left">
<div class="form-horizontal">
@Html.HiddenFor(model => model.CategoryID, new { @id = "hdnCategoryId" })
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.CategoryName, new { @class = "control-label col-md-2" })
<div class="col-md-4">
@Html.TextBoxFor(model => model.CategoryName, new { @class = "form-control", @id = "txtCategoryName" })
@Html.ValidationMessageFor(model => model.CategoryName)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Description, new { @class = "control-label col-md-2" })
<div class="col-md-4">
@Html.TextBoxFor(model => model.Description, new { @class = "form-control", @id = "txtDescription" })
@Html.ValidationMessageFor(model => model.Description)
</div>
</div>
<div class="form-group">
<div class="col-md-2"> </div>
<div class="col-md-4">
<a id="btnSaveCategory" class="btn btn-primary btn-saveCategory" name="btnSave">Save</a>
<a id="btnCancel" class="btn btn-white btn-cancelCategory" name="btnCancel">Cancel</a>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
<script>
$(document).ready(function () {
debugger;
$("#btnSaveCategory").click(function () {
debugger;
$("#frmAddEditCategory").removeData("validator");
$("#frmAddEditCategory").removeData("unobtrusiveValidation");
$.validator.unobtrusive.parse("#frmAddEditCategory");
if ($("#frmAddEditCategory").valid()) {
$.ajax({
url: "@Url.Action("SaveCategory", "Category")",
type: "post",
data: $("#frmAddEditCategory").serialize(),
success: function (data) {
debugger;
$('#main-content').html(data);
},
error: function (a,b,c) {
$('#main-content').html('there is error while submit');
}
});
}
});
$("#btnCancel").click(function () {
$('#main-content').load("@Url.Action("Index", "Category")");
});
});
</script>
my route is not shown when I click on the category button. it only shows the base url and the # sometime if button is clicked. how can i show the route and not refresh the page. I know using angular or any framework its easy to implement but how can we do that using just asp.net mvc .
This usually happens on my edit and create page my edit method is
[HttpPost]
//[Route("CategoryEdit/{id}/Edit")]
//[Route("CategoryEdit/{id}")]
//[Route("CategoryEdit/{id:range(1, 3)}")]
// GET: /Category/Edit/5
public ActionResult Edit(int? id)
{
var category = categoryService.GetCategory(id.Value);
return ViewResult("_CategoryAddEdit", category); ;
}
When I enable attribute routing it stops calling my Controller method.