嗨,我是MVC Razor的新手
我正在尝试使用3按钮插入,删除和更新来执行页面。
我的观点看起来像
<div>
@using (Html.BeginForm())
{
<input type="button" class="button1" value="Insert" />
<input type="button" class="button1" value="Delete" />
<input type="button" class="button1" value="Update" />
}
</div>
我的控制器就像
public ActionResult Index()
{
return View();
}
我将如何在控制器中获取这些按钮的事件,以便我能够为每个按钮编写逻辑。请帮助我获取有助于解决此问题的编码或任何合适的链接。
由于 圣
答案 0 :(得分:8)
给按钮命名:
<div>
@using (Html.BeginForm())
{
<input type="button" name="button" class="button1" value="Insert" />
<input type="button" name="button" class="button1" value="Delete" />
<input type="button" name="button" class="button1" value="Update" />
}
</div>
在你的行动中使用它:
public ActionResult Index(string button)
{
switch (button)
{
case "Insert": ...
case "Update": ...
case "Delete": ...
}
return View();
}
尽管如此,我不会使用这种方法。我会动态修改表单操作到不同的控制器操作(使用Url.Action
)将该代码挂钩到使用jQuery单击处理程序。
答案 1 :(得分:4)
[编辑]
在其他答案的注释中重新改变我的观点:当你将逻辑BIND LOGIC转换为C#中的按钮的值时,你将C#代码绑定到该语言。
想象一下,您在英文版中有“保存”按钮:
<input type="submit" value="Insert" name='button' />
在您的代码中,您将使用值切换:
public ActionResult Index(string button)
{
switch (button)
{
case "Insert": ...
case "Update": ...
case "Delete": ...
}
return View();
}
现在 - 当用另一种语言查看该表格时 - 您认为会发生什么?!?!
这是威尔士html输出:
<input type="submit" value="Mewnosod" name='button' />
和德国人:
<input type="submit" value="Einfügen" name='button' />
这个EVER如何发挥作用?!
全球化不是一个单独的问题!!!!
如果您使用此方法,您的操作将如下所示:
public ActionResult Index(string button)
{
switch (button)
{
case "Insert": ...
case "Update": ...
case "Delete": ...
case "Einfügen": ...
case "Mewnosod": ....
.... a load of other languages for each action type -
}
return View();
}
请......认真......
[/编辑]
这是我的MVC动作选择器代码:Asp.Net Mvc action selector
本质上你需要一个动作选择器类:
/// <summary>
/// AcceptParameterAttribute to enable submit buttons to execute specific action methods.
/// </summary>
public class AcceptParameterAttribute : ActionMethodSelectorAttribute
{
/// <summary>
/// Gets or sets the value to use in submit button to identify which method to select. This must be unique in each controller.
/// </summary>
public string Action { get; set; }
/// <summary>
/// Determines whether the action method selection is valid for the specified controller context.
/// </summary>
/// <param name="controllerContext">The controller context.</param>
/// <param name="methodInfo">Information about the action method.</param>
/// <returns>true if the action method selection is valid for the specified controller context; otherwise, false.</returns>
public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
{
if (controllerContext == null)
{
throw new ArgumentNullException("controllerContext");
}
var req = controllerContext.RequestContext.HttpContext.Request;
return req.Form.AllKeys.Contains(this.Action);
}
}
这取决于你给按钮的名字。
然后您可以使用以下命令修饰操作:
[AcceptParameter(Action = "Edit")]
public ActionResult Person_Edit(PersonViewModel model){
...
}
切换动作很脏 - 这是一种更清洁的方法。我认为也更自然。
答案 2 :(得分:1)
我建议你阅读一些关于MVC3的简单教程 - 它将让你有机会理解MVC框架的基本原理。你可以开始here。
现在回答你的问题:最好的方法是从每个按钮调用控制器中的一些动作。
@Html.ActionLink("Insert", "Insert")
或强>
@Html.ActionLink("Edit", "Edit", new{Id=lineId}).
然后,当用户点击此链接时 - 他将获得正确的视图,该视图已准备好处理该任务。
答案 3 :(得分:1)
如果你像我一样使用本地化
,这是改进的AcceptParameterAttributepublic class AcceptParameterAttribute : ActionMethodSelectorAttribute
{
public string Name { get; set; }
public Type ResourceType { set; get; }
public string Value { get; set; }
public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
{
HttpRequestBase request = controllerContext.RequestContext.HttpContext.Request;
if (ResourceType != null)
{
ResourceManager resourceManager = new ResourceManager(ResourceType);
return request.Form[Name] == resourceManager.GetString(Value);
}
return request.Form[Name] == Value;
}
}
类似于DisplayAttribute的用法
[AcceptParameter(ResourceType = typeof(Resources), Name = "submit", Value = "Search")]
和按钮
<input type="button" name="submit" value="@Resources.Search" />