如何在mvc中的Homecontroller内的条件下更改提交按钮文本?

时间:2012-04-21 16:03:07

标签: asp.net-mvc asp.net-mvc-3 entity-framework stored-procedures

我有mvc 3应用程序,其中我在Index.cshtml视图上有一个输入表单。 还有一个带有editdelete按钮

的网络网格

取决于我需要更改提交按钮文本的这些操作链接。 我如何在homecontroller.cs中实现这一点?仅为所有editinsert使用一个视图。

检查homecontroller.cs中的useraction

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

                    )
    }

2 个答案:

答案 0 :(得分:3)

enter code here在控制器中设置ViewBag属性:

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

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

..然后在视图中显示值:

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

答案 1 :(得分:2)

一个简单的解决方案是将标签文本作为属性放在Controller的ViewBag中,但这可能不是最好的方法......