2017-01-19 10 views
0

ボタンを2つ使用してユーザー入力が必要なフォームがあります.1つのボタンはsubmitフォームの値に、もう1つのボタンはpartial viewをロードします。私のアプローチが正しいかどうかはわかりません。 submitボタンは完全にうまく動作しますが、GenerateBtn IDのボタンをクリックしても、partial viewをロードするDisplayProposal()関数は起動しません。どのようなアイデアを解決するか、より良いアプローチ?ASP.NET MVC Button.Click JavaScriptで動作しません

@using (Html.BeginForm()) 
{ 
@Html.AntiForgeryToken() 

<div class="form-horizontal"> 
    <h4>Proposal</h4> 
    <hr /> 
    @Html.ValidationSummary("", new { @class = "text-danger" }) 
    @Html.HiddenFor(model => model.id) 

    <div class="form-group"> 
     @Html.LabelFor(model => model.item, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.item, new { htmlAttributes = new { @class = "form-control" } }) 
     </div> 
    </div> 

    <h2>Generated Questions</h2> 
    <div id="ProposalTable"></div> 

    <div class="form-group"> 
     <div class="col-md-offset-2 col-md-10"> 
      <input type="submit" value="Save" class="btn btn-default" /> 
     </div> 
    </div> 
</div> 

<div class="form-group"> 
    <div class="col-md-offset-2 col-md-10"> 
     <input type="button" value="Generate" class="btn btn-default" id="GenerateBtn" /> 
    </div> 
</div> 
} 

@section Scripts { 
    @Scripts.Render("~/bundles/jqueryval") 

<script> 
    document.getElementById("GenerateBtn").onclick = function() { DisplayProposal() }; 
    function DisplayProposal() { 
     //Loads Partial View to <div>ProposalTable</div> 
     $('ProposalTable').load('@Url.Action("LoadStuff", "Proposals")'); 
    } 

</script> 

}

+0

がClientIDModeには= "静的" ''の

+0

を配置しようと'追加関数定義の後のdocument.getElementById行 – Steve

答えて

1

あなたはjqueryのを使用していない理由:

$(document).ready(function(){ 
    $("#GenerateBtn").click(function(){ 
     DisplayProposal(); 
    }); 
}); 
+0

ありがとう@MikeSそれは完璧に動作します!上記のコードの代わりにこれが動作する理由は何ですか? – Pow4Pow5

+0

頭の上から外れません。 document.getElementById( "GenerateBtn")は何も返しませんか? – MikeS

関連する問題