Dear All,
How to call jquery function from mvc controller
I tried the code you pointed to but still i wasnt succesfull. here is my code below.
@model BOL.tbl_categories_master
@{
ViewBag.Title = "Add category";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Add New Category</h2>
@using (Html.BeginForm("AddCategory","Category",FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.main_category,"Name", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.main_category, new { htmlAttributes = new { @class = "form-control",id="main_category" } })
@Html.ValidationMessageFor(model => model.main_category, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" id="btnAdd" class="btn btn-primary" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/jquery.validate.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
<script src="~/Scripts/toastr.js"></script>
<link href="~/Content/toastr.css" rel="stylesheet" />
<script type="text/javascript">
function ShowSuccessMsg() {
toastr.success("Successfully Added.");
};
function ShowFailure(Msg) {
toastr.error("Error:" + Msg);
};
</script>
@if (ViewBag.JavaScriptFunction != null)
{
<script type="text/javascript">
@Html.Raw(ViewBag.JavaScriptFunction)
</script>
}
public ActionResult AddCategory(tbl_categories_master CategoryInfo)
{
try
{
if (ModelState.IsValid)
{
objBs.CategoryBs.Insert(CategoryInfo);
return ViewBag.JavaScriptFunction = string.Format("ShowSuccessMsg();");
//return RedirectToAction("Create");
}
else
{
return View("Create");
}
}
catch (Exception ex)
{
ViewBag.JavaScriptFunction = string.Format("ShowFailure('{0}');", ex);
return RedirectToAction("Create");
}
}