javascript - ASP.NET MVC5, JSON, popup partialView - set DropDownList selected -
i want set ddl selected item item inserted in popup page. used script sample here - http://www.advancesharp.com/blog/1126/search-sort-paging-insert-update-and-delete-with-asp-net-mvc-and-bootstrap-modal-popup-part-2
in jsonresult see inserted value (checking in debug mode breakpoint), need setting value selected in ddl in main page.
public actionresult workplaces(int id = 0) { var workplace = new work(); return partialview("workplaces", workplace); } [httppost] public jsonresult workplaces(work work) { if (modelstate.isvalid) { db.works.add(work); db.savechanges(); return json(new { success = true }); } return json(work, jsonrequestbehavior.allowget); } model classes
public class person { public int id { get; set; } [display(name = "person name")] [required(allowemptystrings = false, errormessage = "name required")] public string name { get; set; } public nullable<int> workid { get; set; } public virtual work work { get; set; } } public class work { public int id { get; set; } [display(name = "work place")] [required(allowemptystrings = false, errormessage = "name required")] public string workplace { get; set; } } main page
@model testappauth.models.person @{ viewbag.title = "create"; } <h2>create</h2> @using (html.beginform()) { @html.antiforgerytoken() <div class="form-horizontal"> <h4>person</h4> <hr /> @html.validationsummary(true, "", new { @class = "text-danger" }) <div class="form-group"> @html.labelfor(model => model.name, htmlattributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.editorfor(model => model.name, new { htmlattributes = new { @class = "form-control" } }) @html.validationmessagefor(model => model.name, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @html.labelfor(model => model.workid, "workid", htmlattributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.dropdownlist("workid", null, htmlattributes: new {@class = "form-control"}) @html.validationmessagefor(model => model.workid, "", new {@class = "text-danger"}) <a class="btn btn-success" data-modal="" href="/person/workplaces" id="btncreate"> <span class="glyphicon glyphicon-plus"></span> </a> </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="create" class="btn btn-default" /> </div> </div> </div> } <div> @html.actionlink("back list", "index") </div> <!-- modal placeholder--> <div id='mymodal' class='modal fade in'> <div class="modal-dialog"> <div class="modal-content"> <div id='mymodalcontent'></div> </div> </div> </div> @section scripts{ @scripts.render("~/scripts/appjs/workplace.js") } popup page
@model testappauth.models.work <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> <h3 class="modal-title">add new workplace</h3> </div> @using (html.beginform()) { @html.antiforgerytoken() <div class="form-horizontal"> <h4>work place</h4> <hr /> @html.validationsummary(true, "", new { @class = "text-danger" }) <div class="form-group"> @html.labelfor(model => model.workplace, htmlattributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.editorfor(model => model.workplace, new { htmlattributes = new { @class = "form-control" } }) @html.validationmessagefor(model => model.workplace, "", new { @class = "text-danger" }) </div> </div> </div> <div class="modal-footer"> <span id="progress" class="text-center" style="display: none;"> <img src="/images/wait.gif" alt="wiat" /> wait.. </span> <input class="btn btn-primary" type="submit" value="save" /> <button class="btn btn-warning" data-dismiss="modal">close</button> </div> } <script> $("form").removedata("validator"); $("form").removedata("unobtrusivevalidation"); $.validator.unobtrusive.parse("form"); </script> script popup
$(function () { $.ajaxsetup({ cache: false }); $("a[data-modal]").on("click", function (e) { $('#mymodalcontent').load(this.href, function () { $('#mymodal').modal({ keyboard: true }, 'show'); bindform(this); }); return false; }); }); function bindform(dialog) { $('form', dialog).submit(function () { $('#progress').show(); $.ajax({ url: this.action, type: this.method, data: $(this).serialize(), success: function (result) { if (result.success) { $('#mymodal').modal('hide'); $('#progress').hide(); location.reload(); } else { $('#progress').hide(); $('#mymodalcontent').html(result); bindform(); } } }); return false; }); } also, script set [required] attribute in model classes, closes if click close button without filling fields. how make not close?
for question how validate before closing model use below code
$(function () { $('#mymodal').on('hide.bs.modal', function (event) { var form = $("#formid"); form.validate(); if(form.valid()){ //ajax; } else{ event.preventdefault(); }})}); now in ajax success used jquery add value dropdown
Comments
Post a Comment