0

I have a form that requires user input with two buttons: one button to submit the form values and the other button is to load a partial view. I'm not sure if my approach here is correct though. The submit button works perfectly fine however clicking the button with GenerateBtn id does not fire the DisplayProposal() function that loads the partial view. Any idea to solve or a better approach??

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

}

asked Jan 19, 2017 at 16:17
2
  • add ClientIDMode = "Static" in <input type="button" Commented Jan 19, 2017 at 16:19
  • try to put the document.getElementById line after the function definition Commented Jan 19, 2017 at 16:29

1 Answer 1

1

Any reason you aren't using jquery:

$(document).ready(function(){
 $("#GenerateBtn").click(function(){
 DisplayProposal();
 });
});
answered Jan 19, 2017 at 16:21
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @MikeS It works perfectly fine! Any Reason why this works instead of the code above?
Not off the top of my head. does document.getElementById("GenerateBtn") return anything?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.