在MVC中提交后不想重定向同一页面 [英] Do not want to redirect the same page after submit in MVC
问题描述
目前,在主视图中单击提交按钮后,部分将使用AJAX提交。但我不希望提交后同样部分重定向。我怎么能摆脱这个。我是MVC的新手,所以没有太多想法。我只是在[HttpPost] ActionResult中返回了部分内容。我认为,这不是正确的做法。请指导我。
我尝试了什么:
控制器< br $>
[HttpPost]
public ActionResult Home(ClsAA clsAA)
{
ModelState.Clear();
if(Id ==Z01)
{
返回PartialView(〜/ Views / Home / _P.cshtml,clsAA);
}
else if(Id ==P02)
{
返回PartialView(〜/ Views / Home / _I.cshtml,clsAA);
}
返回PartialView();
}
主视图
<表>
< tr>
< th> @ Html.DisplayNameFor(m => m.ProductName)< / th>
< th> @ Html.DisplayNameFor(m => m.ProductDetail)< / th>
< th> @ Html.DisplayNameFor(m => m.ProductCost)< / th>
< / tr>
@foreach(模型中的var项目)
{
< tr>
< td> @ Html.DisplayFor(modelItem => item.ProductName)< / td>
< td> @ Html.DisplayFor(modelItem => item.ProductDetail)< / td>
< td> @ Html.DisplayFor(modelItem => item.ProductCost)< / td>
< td>< input class =search btn-defaulttype =buttonvalue =选择data-assigned =@ item.ProductCode/>< / td>
< / tr>
}
< / table>
< div id =MyReports>
@using(Html.BeginForm(Home,Home,FormMethod.Post,new {id =myform}))
{
< div id =mypartial >< / DIV>
< button type =submitid =submit>运行< / button>
}
< / div>
这是部分加载的方式
$('。search')。click(function(){
var id = $(this).data('assigned');
var route ='@ Url。动作(PartialView,Home)?id ='+ id;
$('#mypartial')。load(route)
});
部分查看
< div class =PContainer>
< label>
@ Html.RadioButtonFor(m => m.HDT,H,new {@checked =checked})H
< / label>
< label>
@ Html.RadioButtonFor(m => m.HDT,T)T
< / label>
< / div>
< div class =P>
< div id =Hclass =PHT>
@ Html.TextBoxFor(m => m.txtH)
< / div>
< div id =Tclass =PHT>
@ Html.TextBoxFor(m => m.txtT)
< / div>
< / div>
提交的脚本
$ (document).ready(function(){
var url ='@ Url.Action(Home)';
$('#myform')。submit(function(){
if(!$(this).valid()){
return;
}
$ .post(url,$(this).serialize(),function(response){
调试器;
$('#mypartial')。html(响应);
$(#myModal)。modal('show');
});
返回false;
})
});
('。search')。click (function(){
var id =
(this).data('assigned');
var route ='@ Url.Action(PartialView ,Home)?id ='+ id;
('#mypartial')。load(route)
});
部分查看
< div class =PContainer>
< label>
@ Html.RadioButtonFor(m => m.HDT,H,new {@checked =checked})H
< / label>
< label>
@ Html.RadioButtonFor(m => m.HDT,T)T
< / label>
< / div>
< div class =P>
< div id =Hclass =PHT>
@ Html.TextBoxFor(m => m.txtH)
< / div>
< div id =Tclass =PHT>
@ Html.TextBoxFor(m => m.txtT)
< / div>
< / div>
提交的脚本
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.
What I have tried:
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
<table> <tr> <th>@Html.DisplayNameFor(m => m.ProductName)</th> <th>@Html.DisplayNameFor(m => m.ProductDetail)</th> <th>@Html.DisplayNameFor(m => m.ProductCost)</th> </tr> @foreach (var item in Model) { <tr> <td>@Html.DisplayFor(modelItem => item.ProductName)</td> <td >@Html.DisplayFor(modelItem => item.ProductDetail)</td> <td>@Html.DisplayFor(modelItem => item.ProductCost)</td> <td><input class="search btn-default" type="button" value="Select" data-assigned="@item.ProductCode"/></td> </tr> } </table> <div id="MyReports"> @using (Html.BeginForm("Home", "Home", FormMethod.Post, new { id = "myform" })) { <div id="mypartial"></div> <button type="submit" id="submit">Run</button> } </div>
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
<div class="PContainer"> <label> @Html.RadioButtonFor(m => m.HDT, "H", new { @checked = "checked" })H </label> <label> @Html.RadioButtonFor(m => m.HDT, "T")T </label> </div> <div class="P"> <div id="H" class="PHT"> @Html.TextBoxFor(m => m.txtH) </div> <div id="T" class="PHT"> @Html.TextBoxFor(m => m.txtT) </div> </div>
Scripts for submit
$(document).ready(function () { var url = '@Url.Action("Home")'; $('#myform').submit(function () { if (!$(this).valid()) { return; } $.post(url, $(this).serialize(), function (response) { debugger; $('#mypartial').html(response); $("#myModal").modal('show'); }); return false; }) });
('.search').click(function () { var id =
(this).data('assigned'); var route = '@Url.Action("PartialView", "Home")?id=' + id;
('#mypartial').load(route) });
Partial View
<div class="PContainer"> <label> @Html.RadioButtonFor(m => m.HDT, "H", new { @checked = "checked" })H </label> <label> @Html.RadioButtonFor(m => m.HDT, "T")T </label> </div> <div class="P"> <div id="H" class="PHT"> @Html.TextBoxFor(m => m.txtH) </div> <div id="T" class="PHT"> @Html.TextBoxFor(m => m.txtT) </div> </div>
Scripts for submit
这篇关于在MVC中提交后不想重定向同一页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!