asp.net-mvc - 如何在 mvc 中的 Homecontroller 内根据条件更改提交按

我有一个 mvc 3 应用程序,其中我在 Index.cshtml View 上有一个输入表单。 还有一个 webgrid,它有 edit,delete 按钮

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

检查 homecontroller.cs 中的用户操作

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

        }


        if (userAction == "Delete" )
        {

        }

    }

View code: 
@model Mapping.Models.SecurityIdentifierMappingViewModel
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Mapping</legend>
        <div class="editor-label">
            @Html.Label("Pricing SecurityID")
        </div>
        <div class="editor-field">
            @Html.HiddenFor(model => model.MappingControls.Id)
            @Html.DropDownListFor(model => model.MappingControls.PricingSecurityID,
         new SelectList(Model.PricingSecurities, "Value", "Text"),
         "Select SecurityID"
            )
            @Html.ValidationMessageFor(model => model.MappingControls.PricingSecurityID)
        </div>
        <div class="editor-label">
            @Html.Label("CUSIP ID")
        </div>
        <div class="editor-field">
            @Html.DropDownListFor(model => model.MappingControls.CUSIP,
         new SelectList(Model.CUSIPs, "Value", "Text"),
            "Select CUSIP"
            )
            @Html.ValidationMessageFor(model => model.MappingControls.CUSIP)
        </div>

        <div class="editor-label">
            @Html.Label("Calculation")
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.MappingControls.Calculation)
            @Html.ValidationMessageFor(model => model.MappingControls.Calculation)
        </div>
        <p>
            <input type="submit" value="Insert" />
        </p>
    </fieldset>
}

on same page i have a webgrid I need to change text of submit button to Update when I clicked on webgrid's edit button. i'm newbie.

Webgrid code
    @model IEnumerable<Mapping.Models.SecurityIdentifierMapping>
    @{
        ViewBag.Title = "Mapping";
        WebGrid grid = null;
        if (Model.Count() > 0)
        {
            grid = new WebGrid(source: Model,
                                    defaultSort: "Id",
                                    canPage: true,
                                    canSort: true,
                                    rowsPerPage: 10);
        }
    }
    <h3>
        Mapping Web Grid</h3>
    @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>),
                                                grid.Column("PricingSecurityID"),
                                                grid.Column("CUSIP"),
                                                grid.Column("Calculation")
                                              )

                    )
    }

最佳答案

在此处输入代码在 Controller 中设置一个 ViewBag 属性:

public ActionResult
Index(string userAction) 
    { 
       if (userAction == "Edit"
) 
        {
   ViewBag.SubmitValue = "Edit";
        } 

        if (userAction == "Delete" ) 
        {
 ViewBag.SubmitValue = "Delete";
        } 
    }

..然后在 View 中显示值:

<input type="submit" value="@ViewBag.SubmitValue" />

关于asp.net-mvc - 如何在 mvc 中的 Homecontroller 内根据条件更改提交按钮文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10260645/

相关文章:

ms-access - Access : Truncation error when appendi

perl - Perl 中的非本地返回(从调用者返回)

maven - 你如何使用 Maven 设置 jetty-start?

sql - Oracle CONNECT BY 分层查询中对父列的引用

php - MongoDB PHP 认证

jquery - 我如何使用 jQuery 找到 parent ?

php - 清理 XSS 到 Textarea 的输出

winapi - 如何更新进度条?

r - 在 R 中从循环或 lapply 按名称调用列表

visual-studio-2010 - 如何断开 VisualSVN 与我的解决方案的连接?