Untitled
unknown
plain_text
2 months ago
2.4 kB
10
Indexable
/* cshtml create */ <div id="CreateUser" class="form-horizontal"> <h4>Users</h4> <hr /> @Html.ValidationSummary(true, "", new { @class = "text-danger" }) <div class="form-group"> <label class = "control-label col-md-2">Name</label> <div class="col-md-10"> <input class="form-control" type="string" name="ListingName" id="ListingName"/> </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <label type="button" name="CreateUser" id="btnCreateUser" value="CreateUser" class="btn btn-default" for="ListingName"><input type="button" name="CreateUser" id="btnCreateUser" value="CreateUser" class="btn btn-default" onclick="CreateUser(@listingMd.Id, @listingMd.ListingName)"/></label> </div> </div> </div> <script type="text/javascript"> $("#btnCreateUser").click(function () { var ListingDBTable = {}; ListingDBTable.ListingName = $("#ListingName").val(); $.post("/Home/CreateUser", { ListingDBTable: ListingDBTable }, function (data) { if (data != null) { alert("User Created"); location.reload(); //for refreshing the page after saving } else { alert("Error"); } }); }) </script> /*create controller method */ [HttpPost] public JsonResult CreateUser(ListingProjects createListingProjects) { try { _context.ListingDBTable.Add(createListingProjects); _context.SaveChanges(); return Json(createListingProjects); //returning user to javacript } catch (Exception ex) { return null; } } [AllowAnonymous] /* once the user input is successfully saved this method should display the newly saved item via create method*/ public async Task<IActionResult> TestDashboard1() { var datasource = _context.ListingDTO_DBTable.AsQueryable(); var query = datasource .Select(x => new ListingProjectsDTO() { Id = x.Id, ListingName = x.ListingName, }); var listings = await query.ToListAsync(); return View(listings); }
Editor is loading...
Leave a Comment