2012-04-18 12 views
0

私はmvcアプリケーションを持っています。私は入力を取っているフォームを持っています。そして、submitをクリックすると、データベースに値を更新しています。同じページにmvc3でのコード作成時に送信ボタンテキストを変更するにはどうすればよいですか?

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

私はWebGridの編集ボタンをクリックしたときUpdateにsubmitボタンのテキストを変更する必要があるのWebGridを持っています。 私は初心者です。また

<input id="btn-submit" type="submit" value="Insert" /> 

あなたの編集アンカークラス適用されます:編集リンクのクリックイベントをサブスクライブし、その後

grid.Column(
    "", 
    header: null, 
    format: 
     @<text> 
      @Html.ActionLink(
       "Edit", 
       "Index", 
       new { uid = (int)item.id, userAction = "Edit" }, 
       new { @class = "edit" } 
      ) 
      @Html.ActionLink(
       "Delete", 
       "Index", 
       new { uid = (int)item.id, userAction="Delete" }, 
       new { @class = "Delete" } 
      ) 
     </text> 
    ) 

をして

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

        ) 
    } 
    <script type="text/javascript"> 
     $(function() { 
      $(".Delete").click(function() { 
       if (confirm("Do you want to delete?")) { 
        var href = $(".Delete").attr('href'); 
        href = href + "?userAction=Delete"; 
        window.location.href = href; 
        return true; 
       } 
       return false; 
      }); 
     }); 
    </script> 

答えて

2

あなたの送信ボタンにIDを与えることができます。

$(function() { 
    ... 
    $('.edit').click(function() { 
     $('#btn-submit').val('Update'); 
     ... 
    }); 
}); 

編集アンカーはサーバー上で別のアクションを呼び出しているようで、AJAXを使用している場合を除き、ページ全体が再ロードされる可能性があります.JavaScriptで送信ボタンのテキストを設定するのは良い考えではありません。この場合、適切な処理方法は、Edit Controllerアクションでビューモデルにいくつかのプロパティを設定して、編集モードにあることを示し、送信ボタンのテキストに条件を設定できるようにすることです。編集アクションでtrueにあなたのビューモデルのIsEditingプロパティを設定するのはあなた次第

<input type="submit" value="@(Model.IsEditing ? "Update" : "Insert")" /> 

:このような

何か。

+0

このコードはどこに書くべきですか?$( '#btn-submit')。val( 'Update'); 私もwebgridで削除しているので、どうすれば比較できますか? – Neo

+0

うまく働いていますが、ラップリップが起こったとしています。(1秒間、 'Update'に変更すると、何をするのか 'insert'になるでしょうか) – Neo

+0

ビューモデルを使用して、私の更新された回答を参照してください。 –

関連する問題