MVC剃刀按钮 [英] MVC Razor Buttons

查看:98
本文介绍了MVC剃刀按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我是新来的MVC剃刀

Hi i am new to MVC Razor

我试图做一个页面3按钮插入,删除和更新。

I was trying to do a page with 3 Button insert,delete and update.

我的观点看起来像

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

我怎样才能得到这些按钮的事件在我的控制器,这样我就能写出逻辑为每个按钮。请帮我获得的编码或任何合适的链接,这将有助于解决这一问题。

How i will get the events of these button in my controller so that i will be able to write the logic for each button. Please help me to get the coding or any suitable link which will help to solve this problem.

谢谢

推荐答案

重新itterate在其他答案的评论我的观点:当您绑定的逻辑在C#中你是你的C#code绑定到该语言的按钮的值。

to re-itterate my point in the comments on the other answers: when you BIND LOGIC to the VALUE of a BUTTON in C# you are binding your C# code to that language.

想象一下,你在英文版本的保存按钮:

Imagine you have the Save button in the english version as:

<input type="submit" value="Insert" name='button' />

和在你的code你会使用值来切换:

and in your code you are going to switch using the value:

public ActionResult Index(string button)
{
    switch (button)  
    {
        case "Insert": ...
        case "Update": ...
        case "Delete": ...       
    }

    return View();
}

现在 - 当这种形式在另一种语言查看 - 你觉得什么会发生。

Now - when that form is viewed in another language - what do you think will happen?!?!

这里是威尔士HTML输出:

here is the welsh html output:

<input type="submit" value="Mewnosod" name='button' />

和德国的:

<input type="submit" value="Einfügen" name='button' />

这怎么EVER去上班?!

How is that EVER going to work?!

全球化不是一个单独的发行!!!!

Globalization IS NOT A SEPERATE ISSUE!!!!

你的行动看起来像这样如果你使用这个方法:

your action will look like this if you use this method:

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

请认真... ....

please... seriously....

[/编辑]

下面是我的MVC的行动选择code:的 Asp.Net的mvc动作选择

Here's my MVC Action Selector code: Asp.Net Mvc action selector

在本质上你需要一个动作选择器类:

In essence you need an action selector class:

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

这工作了你给该按钮的名称。

Which works off the name you give the button.

您可以再与装饰动作:

[AcceptParameter(Action = "Edit")]
public ActionResult Person_Edit(PersonViewModel model){
...
}

开关在动作脏 - 这是一个更清洁的方式。我觉得更自然了。

Switching in the action is dirty - this is a much cleaner approach. I think much more natural too.

这篇关于MVC剃刀按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆