user10237424
user10237424

Reputation:

Is it possible to pass the parameter value to another parameter inside the button?

Currently, I am working on MVC, and it is my first time coding with this.

I have a parameter in a button that could pass a parameter to a controller. I wanted to pass it to another parameter of a button.

View:

<div style="margin-top:10px;">
    <button type="submit" class="btn" name="button" value="account" id="account">ユーザ情報</button>
</div>

<div style="margin-top:10px;">
    <button type="submit" class="btn" name="button" value="svAccount" id="account">SV情報</button>
</div>

Controller:

[HttpPost]
public ActionResult MaintenanceMenu(string button)
{
    ActionResult action = null;

    switch (button)
    {
        case "account":
            action = RedirectToAction("AccountList", "Account", new {@Bvalue="1"});
            break;
        case "svAccount":
            action = RedirectToAction("AccountList", "Account", new {@Bvalue = "5"});
            break;
        default:
            action = RedirectToAction("LogOut", "Login");
            break;
    }

    return action;
}

Now, I wanted to pass the value "5" or "1" if either of the buttons was clicked to this.

<button type="button" class="btn_userfrom" name="button" value="menu" id="btn_create" data-list-url="<%:Url.Action("AccountCreate", "Account", new { @ref_code = @Bvalue }) %>">新規登録</button>

Is that even possible?

Controller:

public ActionResult AccountList(string Id, string Bvalue)
        {       

                UserData userData = Session["info"] as UserData;
                if (userData.RoleCode != "4")
                {
                    return RedirectToAction("LogOut", "Login");
                }

                BusinessLogicResult Result =
                    new AccountListSearchLogic().doProcess(new BusinessLogicInput() {
                        UserInfoData = userData,
                        BvalueAction = Bvalue  
                    });
            var UserList = Result[AccountListSearchLogic.KEY_OUT_USER_LIST] as List<AccountUserListView>;
            return View(UserList);
        }

Upvotes: 0

Views: 89

Answers (2)

Kamil Folwarczny
Kamil Folwarczny

Reputation: 591

If i understand you right. Then you get your value 5 or 1 in AccountList Action, which returns page with final button. You can do what progrAmmar suggest like this.

1. Way - Model - You pass model with value into view where you use it.

In Controller

 public ActionResult AccountList(string value)
 {
      return this.View(value);
 }

In View

 @model string
 <button type="button" class="btn_userfrom" data-list-url="<%:Url.Action("AccountCreate", "Account", new { @ref_code = Model }) %>" name="button" value="menu" id="btn_create" >新規登録</button>

2. Way - ViewBag - You create viewbag which you use in your page. Keep in mind that viewbag will survive only one request.

In Controller

 public ActionResult AccountList(string value)
 {
      ViewBag.Value = value;
      return this.View();
 }

In View

 <button type="button" class="btn_userfrom" data-list-url="<%:Url.Action("AccountCreate", "Account", new { @ref_code = ViewBag.Value }) %>" name="button" value="menu" id="btn_create" >新規登録</button>

EDIT

I would recoment you to create ViewModel with your UserList and BValue for your page.

public class MyViewModel
{
    public MyViewModel(List<AccountUserListView> userList, String bValue)
    {
        this.UserList = userList;
        this.BValue = bValue;
    }

    public List<AccountUserListView> UserList;
    public String BValue;
}

And pass it into your View.

public ActionResult AccountList(string Id, string Bvalue)
{       
    UserData userData = Session["info"] as UserData;
    if (userData.RoleCode != "4")
    {
        return RedirectToAction("LogOut", "Login");
    }

    BusinessLogicResult Result = new AccountListSearchLogic().doProcess(
                                       new BusinessLogicInput() {
                                           UserInfoData = userData,
                                           BvalueAction = Bvalue  
                                       }
                                 );
    var UserList = Result[AccountListSearchLogic.KEY_OUT_USER_LIST] 
                         as List<AccountUserListView>;

    return View(new MyViewModel(UserList, Bvalue));
}

Something like this. And in view:

@model MyViewModel
<button type="button" class="btn_userfrom" data-list-url="<%:Url.Action("AccountCreate", "Account", new { @ref_code = Model.BValue }) %>" name="button" value="menu" id="btn_create" >新規登録</button>

Upvotes: 1

progrAmmar
progrAmmar

Reputation: 2670

You can do it two ways (may be three)

  1. Make the Value part of the Model and post the model to the View so you can do: <button type="button" class="btn_userfrom" name="button" value="menu" id="btn_create" data-list-url="<%:Url.Action("AccountCreate", "Account", new { @ref_code = Model.BValue}) %>">新規登録</button>

  2. Put the value in ViewBag and call it on the View. <button type="button" class="btn_userfrom" name="button" value="menu" id="btn_create" data-list-url="<%:Url.Action("AccountCreate", "Account", new { @ref_code = ViewBag.BValue }) %>">新規登録</button>

  3. Put the value in ViewData and call in on the View. <button type="button" class="btn_userfrom" name="button" value="menu" id="btn_create" data-list-url="<%:Url.Action("AccountCreate", "Account", new { @ref_code = ViewData["BValue"].ToString()}) %>">新規登録</button>

Upvotes: 0

Related Questions