Answered by:
C# MVC View and Modal in View to Controller Action

Question
-
User-544325736 posted
Hello,
I have a view with a table with rows of records. I have a button on each row which brings up that specific record in a modal (the modal is in the same view) on the modal its for editing the data and I have a button that I want to save the modal data and bring into the controller so I can update the record. I'm currently getting controller action cannot be found even though I have the correct spelling and path. I tried POST and GET. Not to sure what I am missing? Here is my View and action.
VIEW (most of it) @model Messer_PartNumbers.Models.ServiceModel @using (Html.BeginForm("TableButtons", "Service", FormMethod.Post)){ <div class="row" style="display:flex; justify-content:space-around; width:50%; margin-left:25%; margin-bottom:30px;"> @* data-targeturl="@Url.Action("Details","Home",new { id = item.Id })"> *@ @*<button type="button" class="btn btn-info" data-toggle="ajax-modal" data-target="#modalSQ" data-url="@Url.Action("OpenSQmodal", "Service")">Btn Modal</button>*@ <button type="button" class="btn btn-info" data-toggle="modal" data-target="#dataModify">Open Modify</button> @*<input type="submit" class="btn btn-primary" name="command" value="Modify" />*@ <input type="submit" class="btn btn-primary" name="command" value="Generate All" formtarget="_blank" /> <input type="button" class="btn btn-primary" name="command" value="Generate Selected" /> <button type="button" class="btn btn-primary" name="TableSettings" data-toggle="modal" data-target="#TableSettings">Table Settings</button> </div> <table class="table-bordered table-hover" style="overflow-y:scroll; max-height:528px; display:block; overflow-x:scroll; background-color:#fff" id="main"> <thead> <tr id="tableHead"> <th style="min-width:50px;"></th> @* PHASE 1 && PHASE 2 *@ @for (int i = 0; i < Model.Settings.Labels.Length; i++) { ///////////////////////////////////////////////////////////////////////////////////////////// if (Model.Settings.Show[i]) { <th style="min-width:50px; ; top:0; background:#fff;">@Model.Settings.Labels[i]</th> } else { <th style="min-width:50px; ; top:0; background:#fff; display:none;">@Model.Settings.Labels[i]</th> } } <th style="min-width: 50px;"></th> </tr> </thead> <tbody> @* ?? PHASE 3 ?? TO-DO: ?? *@ @* Version >= 2.0.6 *@ @* PHASE 2 *@ @for (int i = 0; i < Model.ListQuotes.Count; i++) { var current = Model.ListQuotes[i]; <tr> <td><input type="checkbox" class="checkbox" name="@(i + " Rower")" id="@(i + " Rower")" /></td> @for (int j = 0; j < Model.Settings.Columns.Length; j++) { if (Model.Settings.Show[j]) { var quote = current.GetType().GetProperties().Where(x => x.Name.Equals(Model.Settings.Columns[j])).First(); <td style="padding-right:20px;">@quote.GetValue(current, null)</td> } else { var quote = current.GetType().GetProperties().Where(x => x.Name.Equals(Model.Settings.Columns[j])).First(); <td style="padding-right:20px; display:none;">@quote.GetValue(current, null)</td> } } @*<td><input type="button" class="btn btn-danger" onclick="modify(this.parentElement.parentElement)" value="Edit"/></td>*@ <td> @* class=openModal data-target="#modalInnerSQ"id="" data-toggle="modal"*@ <input type="button" class="openModal btn btn-danger" data-quoteid="@Model.ListQuotes[i].QuoteID" value="Edit" /> @*@Html.ActionLink("Edit", "OpenSQmodal", "Service", new { id=Model.ListQuotes[i].QuoteID }, new { @class="btn btn-danger",@data_toggle="modal", @data_target="modalInnerSQ })*@ </td> </tr> } </tbody> </table> <div class="modal fade" id="modalInnerSQ" tabindex="-1" role="dialog" aria-labelledby="lblSQmodal" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> @*, FormMethod.Get)*@ @using (Html.BeginForm("UpdateServiceTables", "Service", FormMethod.Post, new { model = Model.ServiceVM })) { <div class="modal-header"> <h3 class="modal-title">Service Quote Details</h3> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">×</span> </button> </div> <div class="modal-body"> @Html.ValidationSummary(true, "", new { @class = "text-danger" }) @Html.HiddenFor(x => x.ServiceVM.QuoteID) @Html.HiddenFor(x => x.ServiceVM.CustomerID) @Html.HiddenFor(x => x.ServiceVM.ContactID) @Html.HiddenFor(x => x.ServiceVM.EmpID) <div class="form-group"> @Html.LabelFor(x => x.ServiceVM.CustomerName, htmlAttributes: new { @class = "control-label col-3" }) <div class="col-9"> @Html.TextBoxFor(x => x.ServiceVM.CustomerName, new { @class = "form-control" }) @Html.ValidationMessageFor(x => x.ServiceVM.CustomerName, "", new { @class = "text-danger" }) </div> </div> [DELETED A BUNCH OF PROPERTIES YOU GET THE PICTURE LESS CODE] <div class="form-group"> @Html.LabelFor(x => x.ServiceVM.EmpPosition, htmlAttributes: new { @class = "control-label col-3" }) <div class="col-9"> @Html.TextBoxFor(x => x.ServiceVM.EmpPosition, new { @class = "form-control", @readonly = "readonly" }) </div> </div> //} } } </div> <div class="modal-footer"> <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button> @* SAVE BUTTON [ <button} or form <input> ]*@ <input type="submit" class="btn btn-primary" value="Save" /> </div> } </div> </div> </div> //CONTROLLER ACTION [HttpPost] private ActionResult UpdateServiceTables(ServiceReportViewModel model) { var employee = new Employee_Main { FirstName = model.EmpFirstName, LastName = model.EmpLastName, Email = model.EmpEmail, Position = model.EmpPosition }; /// Search by Email to get EmployeeID to use it var quote = new Serv_Quotes { QuoteID = model.QuoteID, CustomerID = Convert.ToInt32(model.CustomerID), CustomerName = model.CustomerName, ListOfSN2s = model.ListOfSN2s, MachineCount = model.MachineCount, PricePerMachine = model.MachinePrice, Address = model.MachineAddress, City = model.MachineCity, State = model.MachineState, ZipCode = model.MachineZipCode, Country = model.MachineCountry, ExpirationDate = model.ExpirationDate, /// Possibly Search by ContactName to update ContactID ? ContactID = Convert.ToInt32(model.ContactID), ContactName = model.ContactName, ContactEmail = model.ContactEmail, ContactPhone = model.ContactPhone, //EmployeeID = Convert.ToInt32(model.EmpID) }; //finish method / Save / etc... return null; }
I left out the JavaScript it just opens Modal and puts the data in it. Also the JSONResult Controller Action just grabs data from ajax() JavaScript function This is all that should be needed. View + Modal + Controller Action that Modal Button should go to.
Friday, October 18, 2019 2:47 PM
Answers
-
User475983607 posted
Actions must have a public access modifier not private.
[HttpPost] public ActionResult UpdateServiceTables(ServiceReportViewModel model) {
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, October 18, 2019 3:02 PM
All replies
-
User475983607 posted
Actions must have a public access modifier not private.
[HttpPost] public ActionResult UpdateServiceTables(ServiceReportViewModel model) {
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, October 18, 2019 3:02 PM -
User-544325736 posted
I CANNOT BELIEVE I DIDNT SEE THAT OMMGGGGG LMAOOOO I BEEN MESSING AROUND CHANGING STUFF FOR HOURS TRYING TO GET THIS BUT I DIDNT SEE THAT HAHAHAHA
Friday, October 18, 2019 3:44 PM