Asp.net mvc 4 处理一个Html.begin的两个提交按钮的最佳方法
我有一个Html.BeginForm视图,其中包含用户的详细信息:名字、姓氏、电子邮件等。 然后我有两个按钮。批准和拒绝 单击“批准”后,我转到一个视图 当我拒绝时,我会去另一家 处理哪一个被点击的最好方法是什么 我认为:Asp.net mvc 4 处理一个Html.begin的两个提交按钮的最佳方法,asp.net-mvc-4,Asp.net Mvc 4,我有一个Html.BeginForm视图,其中包含用户的详细信息:名字、姓氏、电子邮件等。 然后我有两个按钮。批准和拒绝 单击“批准”后,我转到一个视图 当我拒绝时,我会去另一家 处理哪一个被点击的最好方法是什么 我认为: <% using (Html.BeginForm("PublishGroupsForRecommendedUser", "Recommend", FormMethod.Post, new { id = ViewBag.UserId })) { %> &l
<% using (Html.BeginForm("PublishGroupsForRecommendedUser", "Recommend", FormMethod.Post, new { id = ViewBag.UserId }))
{ %>
<div class="section _100">
<%: Html.LabelFor(model => model.Email)%>
<div>
<%: Html.EditorFor(model => model.Email)%>
<%: Html.ValidationMessageFor(model => model.Email)%>
</div>
</div>
//etc
<input type="hidden" name="action">
<div class="actions">
<div class="actions-right">
<input type="submit" value="Approve" class="submit_button" />
</div>
<div class="actions-left">
<input type="submit" value="Reject" class="submit_button" />
</div>
</div>
<% } %>
因此,我不能使用(Html.BeginForm(“publishgroupsforecommondeduser”,“recomment”,FormMethod.Post,new{id=ViewBag.UserId})来使用该行)
不再,因为根据单击的按钮,我需要转到PublishGroupsForRecommitedDeducer
或RejectUser
操作,该操作将调用相应的视图
有人能给我推荐最好的方法吗?我不完全确定您想要什么,但我认为稍微重新构造一下代码将有助于您的代码: ASP.NET-MVC通过为视图使用特定的ViewModel,可以轻松处理表单中的输入。为要回发的所有内容创建属性: 制作一个简单的POCO对象,如:
public class Person {
public int ID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public bool IsApproved { get; set; }
}
ID在模型上的位置要比在ViewBag中的位置好得多。它是模型的一部分,不要害怕把它放在那里。它也将在获得该职位的结果时填写。非常方便
假设您的第一个操作是:
public ActionResult PersonForm()
{
var model = new Person()
{
ID = WhateverYouWant()
};
return this.View(model);
}
您可以从视图中将其用作模型:
<%@ Page Title="Anything" Language="C#" Inherits="System.Web.Mvc.ViewPage<MVC3ASPX.Models.Person>"... %>
...
...
<% using (Html.BeginForm())
{ %>
<%: Html.ValidationSummary(true) %>
<%: Html.HiddenFor(model => model.ID)%>
<div>
<%: Html.LabelFor(model => model.Name)%>
<div>
<%: Html.EditorFor(model => model.Name)%>
<%: Html.ValidationMessageFor(model => model.Name)%>
</div>
</div>
<div>
<%: Html.LabelFor(model => model.Age)%>
<div>
<%: Html.EditorFor(model => model.Age)%>
<%: Html.ValidationMessageFor(model => model.Age)%>
</div>
</div>
<% } %>
请注意,按钮将显示文本“批准”
和“拒绝”
,但返回的值将是True
或False
,具体取决于您单击的位置
您处理邮件的操作应如下所示:
[HttpPost]
public ActionResult PersonForm(Person model)
{
if (this.ModelState.IsValid) // Validation never hurts
{
if (model.IsApproved)
{
return this.PersonFormApproved(model); // Your first method goes here
}
else
{
return this.PersonFormRejected(model); // Your second goes here
}
}
return this.View(model); // If the model's state is invalid, let's try this one more time!
}
在模型
变量中,您将从表单的值中填充每个属性。另外,由于有一个名为IsApproved的属性,它将由同名的form元素填充。按钮。只有压过的那个
注意,我已将大部分内部逻辑提取到方法:PersonFormApproved
和PersonFormRejected
。这些应该是私有的,以避免程序错误地认为它们是可调用的操作而意外调用
他们应该返回ActionResult
,因为PersonForm
action将返回他们的结果
同时检查
ModelState.IsValid
。仅处理有效的信息。查看您希望如何验证模型。如果您使用jquery,一个解决方案可能是
为表单指定一个id,并将元素中两个提交按钮的操作url设置为数据属性
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "my-form" }))
{
//form elements
<input type="submit" name="action" value="Approve" data-action="@Url.Action("Approve", "YourController")" />
<input type="submit" name="action" value="Reject" data-action="@Url.Action("Reject", "YourController")"/>
}
希望这能有所帮助。在我看来:
<asp:Content ID="Content2" ContentPlaceHolderID="ScriptPlaceHolder" runat="server">
<script type="text/javascript">
function RejectUser(userId) {
$.ajax({
url: '<%= Url.Action("RejectUser", "Recommend") %>',
type: 'GET',
dataType: 'json',
data: { id: userId, reasonForRejection: $('#textarea').val() },
success: function (data) {
window.location.href = data.redirectTo;
}
});
}
</script>
</asp:Content>
<div class="actions">
<div class="actions-right">
<input type="submit" value="Approve" class="submit_button" />
</div>
<div class="actions-left">
<a href="javascript:RejectUser(<%: Model.RecommendedUserId %>);" class="button" id="submit_button">Reject</a>
</div>
</div>
谢谢大家 根据问题中包含的方法,单击“拒绝”按钮时,您希望执行
GET
请求;单击“批准”按钮时,您希望发布表单?如果是,那么就把拒绝按钮转换成一个动作链接(或者一个标签)。这是一个非常好的解释。很好的例子,我喜欢这些描述。你很清楚。我从中学到了很多。
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "my-form" }))
{
//form elements
<input type="submit" name="action" value="Approve" data-action="@Url.Action("Approve", "YourController")" />
<input type="submit" name="action" value="Reject" data-action="@Url.Action("Reject", "YourController")"/>
}
$(function () {
$('#my-form :submit').click(function (e) {
var button = $(this);
button.closest('form').attr('action', button.attr('data-action'));
});
});
<asp:Content ID="Content2" ContentPlaceHolderID="ScriptPlaceHolder" runat="server">
<script type="text/javascript">
function RejectUser(userId) {
$.ajax({
url: '<%= Url.Action("RejectUser", "Recommend") %>',
type: 'GET',
dataType: 'json',
data: { id: userId, reasonForRejection: $('#textarea').val() },
success: function (data) {
window.location.href = data.redirectTo;
}
});
}
</script>
</asp:Content>
<div class="actions">
<div class="actions-right">
<input type="submit" value="Approve" class="submit_button" />
</div>
<div class="actions-left">
<a href="javascript:RejectUser(<%: Model.RecommendedUserId %>);" class="button" id="submit_button">Reject</a>
</div>
</div>
[HttpGet]
public JsonResult RejectUser(int id, string reasonForRejection)
{
if (!String.IsNullOrWhiteSpace(reasonForRejection))
{
Entities.RecommendedUser user = new RecommendedUser();
user = ZincService.UserService.GetRecommendedUserForId(id);
user.ReasonForRejection = reasonForRejection;
ZincService.UserService.UpdateRecommendedUser(user);
ZincService.SaveChanges();
}
return Json(new
{
redirectTo = Url.Action("RecommendedUsers"),
}, JsonRequestBehavior.AllowGet);
}