Events MVC剃须刀按钮

Events MVC剃须刀按钮,events,asp.net-mvc-3,button,razor,Events,Asp.net Mvc 3,Button,Razor,嗨,我是MVC Razor的新手 我试着做一个有3个按钮的页面插入,删除和更新 我的看法是 <div> @using (Html.BeginForm()) { <input type="button" class="button1" value="Insert" /> <input type="button" class="button

嗨,我是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>
如何在控制器中获取这些按钮的事件,以便能够为每个按钮编写逻辑。请帮助我获得编码或任何合适的链接,这将有助于解决这个问题

谢谢
San

为按钮命名:

<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>

尽管如此,我不会使用这种方法。我会动态地修改表单操作,使之成为不同的控制器操作(使用
Url.action
),使用jQuery将代码连接到点击处理程序。

我建议您阅读一些关于MVC3的简单教程-这将使您有机会了解MVC框架的基本原理。你可以开始了

现在回答您的问题:最好的方法是从控制器中的每个按钮调用一些操作

@Html.ActionLink("Insert", "Insert") 

@Html.ActionLink("Edit", "Edit", new{Id=lineId}).
然后,当用户单击此链接时,他将进入正确的视图,该视图已准备好处理该任务。

[编辑]

在对其他答案的评论中重申我的观点:当您将逻辑绑定到C#中按钮的值时,您就是将您的C#代码绑定到该语言

假设您在英文版本中的“保存”按钮为:

<input type="submit" value="Insert" name='button' />
现在-当用另一种语言查看该表单时-你认为会发生什么

以下是威尔士html输出:

<input type="submit" value="Mewnosod" name='button' />
请。。。说真的

[/编辑]

以下是我的MVC操作选择器代码:

本质上,您需要一个操作选择器类:

/// <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);
 
    }
}

在动作中切换是肮脏的-这是一种更干净的方法。我也觉得更自然。

如果您像我一样使用本地化,这里有一个改进的AcceptParameterAttribute

public 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" />


您认为这将如何发布到操作中?HTTP GET不是帖子?!为什么您认为需要POST?否则表单字段将如何发送到服务器?您是否误解了OPs的意图?我没有看到任何有疑问的表单字段。我认为没有必要处理带有三个提交操作按钮的单一表单。从OPs的问题中我们可以看出他想做什么,但最好解释一下他如何做得更好。当然,他可能真的需要你在答案中提供的东西:你的答案是被选中的,所以也许这是真的。顺便说一句,我的答案是基于“我是Razor新手”的部分,所以答案应该是基本的:-)我认为表单字段是隐含的:)这里是该代码的更新版本,支持列表(数据绑定):我已经尝试过这个。。。但我收到一条错误消息,如“Sample.Controllers.AcceptParameterAttribute”不包含接受1个参数的构造函数“oops对不起!我的错误-您需要为mvci使用可选的args方法:[AcceptParameter(Action=“Edit”)]configuration over convention方法认为convention over configuration是我们作为社区的目标否?这就是Ruby如此受欢迎的原因,也是.NET在播放《追赶》的原因:)+1只是为了最后一句话。这就是为什么我讨厌web表单——不是因为技术,而是因为事件驱动的思想如何洗脑了.net web开发人员,我们已经完全忘记了HTTP的意义。MVC的开发人员需要基本上忘却他们的许多web表单的视觉思维!虽然我在上面的评论中是+1:)如果网站是多语言的,这个选项也会失效-这是网站的目的没有?@cvista-有效点,但是你需要理解这是一个假设,开发者应该理解本地化是一个共同的概念,因此,它不需要在一个特定的时间重复answer@cvista-听起来你很渴望获得代表积分。:)无论如何,每个CRUD操作都应该有一个单独的操作方法,最好使用适当的http谓词属性(REST)。URL不需要本地化,因为您应该在以后重定向(PRG)。应该本地化的是按钮文本。所以应该有4种行动方式。
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();
}
/// <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){
...
}
public 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;
    }
}
[AcceptParameter(ResourceType = typeof(Resources), Name = "submit", Value = "Search")]
<input type="button" name="submit" value="@Resources.Search" />