2011-06-15 153 views
0

您好我是MVC剃须刀新手MVC剃刀按钮

我正在试图做一个页面,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(); 
     } 

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

感谢 圣

回答

2

[编辑]

重新itterate我在其他答案的评论点:当您绑定的逻辑在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' /> 

如何有史以来去上班?

全球化不是一个单独的问题!!!!

你的动作会是这样的,如果你用这个方法:

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){ 
... 
} 

切换动作是脏 - 这是一个更清洁的方式。我觉得也更自然。

+0

这里是该代码与列表(数据绑定)支持的更新版本:http://blogs.sonatribe.com/wayne/2011/06/15/171/ – iwayneo 2011-06-15 07:53:38

+0

我试过这个......但我是得到一个错误信息像“Sample.Controllers.AcceptParameterAttribute”不包含一个构造函数,它带有1个参数“ – san 2011-06-15 09:19:08

+0

ooops抱歉!我的错误 - 你需要使用可选的args方法:[AcceptParameter(Action =“Edit”)] – iwayneo 2011-06-15 09:21:11

5

给该按钮的名称:

<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单击处理程序。

+3

+1只是为了最后那句话。这就是为什么我讨厌网络形式 - 不是因为技术,因为事件驱动型思维如何被洗脑.net网络开发人员,我们完全忘记了HTTP的全部内容。 MVC的开发人员需要基本上忽略他们很多Web形式的思维视角。 – RPM1984 2011-06-15 07:56:47

+0

-1因为开关太脏!虽然我对上述评论+1) – iwayneo 2011-06-15 07:59:15

+1

如果网站是多语言的,这个选项也会中断 - Web是不是什么? – iwayneo 2011-06-15 08:00:33

0

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

现在你的问题:最好的办法是从每个按钮调用控制器中的一些动作。

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

@Html.ActionLink("Edit", "Edit", new{Id=lineId}). 

然后,当用户点击这个链接 - 他会去正确看待,这是准备来处理任务。

+0

您认为这会对行动起作用吗? HTTP GET不是POST吗? – iwayneo 2011-06-15 07:46:58

+0

为什么你认为需要POST? – VikciaR 2011-06-16 12:08:55

+0

如何将表单字段发送到服务器?你是否误解了OP的意图? – iwayneo 2011-06-16 14:29:03

0

这里得到改善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" />