Currently, partial is getting submitted using AJAX after submit button click in main view. But I do not want the same partial to redirect after submission. How can I get rid off this. I am new to MVC so do not have much ideas. I have simply returned the partials in [HttpPost] ActionResult. I think, this is not correct way of doing this. Please guide me.
Scripts for submit
- $(document).ready(function () {
- var url = '@Url.Action("Home")';
- $('#myform').submit(function () {
- if (!$(this).valid())
- { return; }
- $.post(url, $(this).serialize(), function (response)
- {
- $('#mypartial').html(response);
- $("#myModal").modal('show'); });
- return false;
- })
- });
Controller
- [HttpPost]
- public ActionResult Home(ClsAA clsAA)
- {
- ModelState.Clear();
- if (Id == "Z01")
- {
- return PartialView("~/Views/Home/_P.cshtml", clsAA);
- }
- else if (Id == "P02")
- {
- return PartialView("~/Views/Home/_I.cshtml", clsAA);
- }
- return PartialView(); }
Main View
@Html.DisplayNameFor(m => m.ProductName) @Html.DisplayNameFor(m => m.ProductDetail) @Html.DisplayNameFor(m => m.ProductCost) - @foreach (var item in Model)
- {
@Html.DisplayFor(modelItem => item.ProductName) @Html.DisplayFor(modelItem => item.ProductDetail) @Html.DisplayFor(modelItem => item.ProductCost) - class="search btn-default" type="button" value="Select" data-assigned="@item.ProductCode"/>
- }
- "MyReports">
- @using (Html.BeginForm("Home", "Home", FormMethod.Post, new { id = "myform" }))
- {
"mypartial">id="submit">Run- }
This is how Partial loads
- $('.search').click(function () {
- var id = $(this).data('assigned');
- var route = '@Url.Action("PartialView", "Home")?id=' + id;
- $('#mypartial').load(route) });
- Partial View
- class="P">"H" class="PHT">
- @Html.TextBoxFor(m => m.txtH)
"T" class="PHT">- @Html.TextBoxFor(m => m.txtT)
2 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Rajesh YadavPosted Apr 19, 2018, 3:40 PM
Naushad AkhtarPosted Apr 18, 2018, 11:30 PM