【问题标题】:How do i chnage submit button text inside mvc3 on condition?如何在条件下更改 mvc3 内的提交按钮文本?
【发布时间】:2012-04-23 14:58:49
【问题描述】:

我有 mvc 3 应用程序,其中我在 Index.cshtml 视图上有一个输入表单。还有一个具有编辑、删除按钮的网络网格

根据这些操作链接,我需要更改我的提交按钮文本。我怎样才能在 homecontroller.cs 中实现这一点?只使用一个视图进行所有编辑、插入。

检查 homecontroller.cs 中的用户操作

public ActionResult Index(string userAction)
    {
       if (userAction == "Edit" )
        {

        }


        if (userAction == "Delete" )
        {

        }

    }

查看代码。

<p>
            <input type="submit" value="Insert" />
</p>

在具有编辑链接的 webgrid 上,删除 在这种情况下,我需要更改提交按钮文本。

@if (grid != null)
    {
        @grid.GetHtml(
                    tableStyle: "grid",
                    headerStyle: "head",
                    alternatingRowStyle: "alt",
                    columns: grid.Columns(
                                                grid.Column("", header: null, format: @<text>@Html.ActionLink("Edit", "Index", new { uid = (int)item.id, userAction = "Edit" })
        @Html.ActionLink("Delete", "Index", new { uid = (int)item.id, userAction="Delete" }, new { @class = "Delete" })</text>),
}

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-3 model-view-controller


    【解决方案1】:

    您可以将userAction 存储在ViewDataViewBag 中并从视图访问它。

    public ActionResult Index(string userAction)
    {
        ViewBag.UserAction = userAction;
    }
    
    
    <input type="submit" value="@(ViewBag.UserAction == "X" ? "Y" : "Z")" />
    

    【讨论】:

    • 但第一次用户操作为空,提交按钮文本应为 'insert plz help'
    • 我确实喜欢这个&lt;input id="btnsubmit" type="submit" value="@(ViewBag.UserAction == "Edit" ? "Edit" : "Insert")" /&gt;,当我运行应用程序时它是插入的,因为用户操作为空。当它是编辑文本时是编辑。只需要2个条件可以吗??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-12
    • 2014-05-03
    • 2015-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-20
    相关资源
    最近更新 更多