开发者

MVC Razor Buttons

Hi i am new to MVC Razor

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

My view looks like

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

And my controller is like

       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.

Thanks San


Give the buttons a name:

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

And use that in your action:

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

    return View();
}

With all that said, I wouldn't use this approach. I'd modify the form action on the fly to different controller actions (using Url.Action) hooking that code up to click handlers using jQuery.


[edit]

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

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

here is the welsh html output:

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

and the german:

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

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

[/edit]

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.

You can then decorate the actions with:

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


Here is improved AcceptParameterAttribute, if you are using localization as I do

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

Usage similar to DisplayAttribute

[AcceptParameter(ResourceType = typeof(Resources), Name = "submit", Value = "Search")]

and button

<input type="button" name="submit" value="@Resources.Search" />


I would suggest you to read some simple tutorial about MVC3 - it will give you opportunity to understand basic principles of MVC framework. You can start here.

Now to your question: best approach is to call from each button some action in controller.

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

or

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

Then, when user clicks this link - he will get to correct view, which is ready to handle that task.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜