Post form data to controller - asp.net-mvc-4

I want to submit the table row values of the rows that are checked to my controller. My controller is always receiving a parameter of value null.
#model IEnumerable<GoogleMapsAPIWeb.Models.Schedule>
#using (Html.BeginForm("Route", "Home", FormMethod.Post))
{
<div>
<table id="schedulerTable">
<thead>
<tr>
<th scope="col"></th>
<th scope="col" >View</th>
<th scope="col">Site Id</th>
</tr>
</thead>
<tbody>
#foreach (var row in Model)
{
<tr>
<td>#Html.CheckBoxFor(c=>row.isSelected)</td>
<td class="smallerDataField">
<a>View</a>#*TODO add code to view*#
</td>
<td><div class="smallerDataField">#row.SiteId</div></td>
</tr>
}
</tbody>
</table>
<input id="btnFindRoute" type="submit" value="Plan Route" />
</div>
}
[HttpPost]
public ActionResult Route(IEnumerable<Schedule> Schedules)
{

Use for loop for this purpose. More info
for (int i = 0; i < Model.Count(); i++)
{
<tr>
<td>#Html.CheckBoxFor(c=> Model[i].isSelected)</td>
</tr>
...
}

Related

MVC5 razor view dosen't display Bootstrap DataTable with #foreach Loop

I want to get a line like this with MVC5 #foreach method and dataTables.
Here is the index.cshtml file.
<table id="example" class="display" style="width:100%">
<thead>
<tr>
<th>
Optionen
</th>
<th>
#Html.DisplayNameFor(model => model.Name)
</th>
<th>
#Html.DisplayNameFor(model => model.Position)
</th>
<th>
#Html.DisplayNameFor(model => model.Office)
</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.Position)
</td>
<td>
#Html.DisplayFor(modelItem => item.Office)
</td>
</tr>
}
</tbody>
</table>
#section scripts
{
#Scripts.Render("~/bundles/jquery")
<script>
$(document).ready(function () {
$('#example').DataTable();
});
}
}
The line with 'Show [ ] entries' and 'Search [ ] ' doesen't appear but the rest of the table.
If there is no foreach loop in the table it works. Has someone an idea how to solve the problem?
Yo need to make sure the number of <th> in <tr> is the same with the number of <td> in <tr>.Here is a demo:
<table id="example" class="display" style="width:100%">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.Position)
</td>
<td>
#Html.DisplayFor(modelItem => item.Office)
</td>
</tr>
}
</tbody>
</table>
result:

How to click on inneHTML String with Selenium?

I am iterating throw a table getting the rows:
<div class="table-responsive">
<table class="table table-striped" id="noteTable">
<thead>
<tr>
<th style="width: 20%" scope="col"></th>
<th style="width: 20%" scope="col">Title</th>
<th style="width: 60%" scope="col">Description</th>
</tr>
</thead>
<tbody>
<tr id="notes" th:each="note : ${notes}">
<td>
<a class="btn btn-success" id="btn-edit-note"
th:onclick="showNoteModal([[${note.id}]],
[[${note.noteTitle}]], [[${note.noteDescription}]])">Edit</a>
<a th:href="#{|/home/delete-note/?title=${note.noteTitle}|}"
id="btn-delete-note" class="btn btn-danger">Delete</a>
</td>
<th scope="row" th:text="${note.noteTitle}"></th>
<td th:text="${note.noteDescription}"></td>
</tr>
</tbody>
</table>
</div>
With this code written below :
public void deleteNote(String title, String description) {
WebDriverWait wait = new WebDriverWait(driver, 2);
WebElement btnDeleteNote;
js.executeScript("arguments[0].click();", tabNotes);
WebElement homeWait = wait.until(webDriver ->
webDriver.findElement(By.id("btn-delete-note")));
for (WebElement note : notes) {
String noteTitle = note.getAttribute("innerHTML");
System.out.println(noteTitle);
if (noteTitle.contains(title)) {
js.executeScript("DELETE NOTE HERE);
}
}
}
When I get the row innerHTML I receive this result as String :
<td>
<a class="btn btn-success" id="btn-edit-note" onclick="showNoteModal(1,
"Note Title Test", "Note Description Test")">Edit</a>
Delete
</td>
<th scope="row">Note Title Test</th>
<td>Note Description Test</td>
- My question is: Thinking that the lines of a Table are Dynamic, how can I click on "< a >" tag element in the string row?
The element has to be unique because it has an id so you can just target it like this:
element = driver.find_element_by_id("btn-delete-note")
print(element.text)

Updating DB-How do I fix "ViewModel is not part of the model for the current context"

I created a view model and associated controller and view, but it looks like there's a piece missing in both my head and my code. When I submit the page, with or without DB changes, I get the following. "The entity type ReportReviewViewModel is not part of the model for the current context."
Models (not all fields)
public partial class RFDS
{
public System.Guid rfds_processing_id { get; set; }
public string location_name { get; set; }
public string address { get; set; }
}
public partial class Sub_Clients_Xref
{
public int id { get; set; }
public string sub_client { get; set; }
public string sub_client_attn { get; set; }
public string sub_client_address { get; set; }
}
public partial class Engineering_License_Xref
{
public int id { get; set; }
public string state_name { get; set; }
public string license_number { get; set; }
}
ViewModel
public class ReportReviewViewModel
{
public RFDS rfds { get; set; }
public Sub_Clients_Xref subclient { get; set; }
public Engineering_License_Xref engineer { get; set; }
public States_Xref state { get; set; }
}
Controller
public ActionResult Review(Guid? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
RFDS rfds_db = db.RFDS.Find(id);
if (rfds_db == null)
{
return HttpNotFound();
}
List<RFDS> rfds1 = db.RFDS.ToList();
List<Sub_Clients_Xref> subclients = db.Sub_Clients_Xref.ToList();
List<Engineering_License_Xref> engineers = db.Engineering_License_Xref.ToList();
List<States_Xref> states = db.States_Xref.ToList();
var viewModel = (from r in rfds1
join s in subclients on r.sub_client equals s.sub_client
join st in states on r.state equals st.state
join e in engineers on r.engineer_name equals e.engineer_name
where r.rfds_processing_id == id && r.sub_client == s.sub_client &&
(r.engineer_name == e.engineer_name && e.state_name == st.state_name)
select new ReportReviewViewModel
{
rfds = r,
subclient = s,
engineer = e
});
return View(viewModel);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Review(ReportReviewViewModel viewModel)
{
if (ModelState.IsValid)
{
db.Entry(viewModel).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Review");
}
return View(viewModel);
}
UPDATED CONTROLLER
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Review(ReportReviewViewModel viewModel)
{
if (ModelState.IsValid)
{
db.Entry(viewModel.rfds).State = EntityState.Modified;
db.Entry(viewModel.engineer).State = EntityState.Modified;
db.Entry(viewModel.subclient).State = EntityState.Modified;
db.Entry(viewModel.state).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Review");
}
VIEW example
#model IEnumerable<TeamWeb.ViewModels.ReportReviewViewModel>
#{
ViewBag.Title = "Review";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="container2">
<div class="nav col-md-3">
#Html.ActionLink("BACK TO LIST", "Index")
</div>
<hr />
<table class="rev-table-main" border="0">
<!----------------------------- EDIT DATA ----------------------------->
#using (Html.BeginForm("Review", "RFDS", FormMethod.Post))
{
#Html.AntiForgeryToken()
foreach (var item in Model)
{
#Html.HiddenFor(model => item.rfds.rfds_processing_id)
<tr>
<td class="rev-values-td">
<table class="rev-table-values" border="0">
#*<tr class="rev-tr">
<td> </td>
</tr>*#
<tr class="rev-tr">
<td style="font-size:medium;font-weight:600;text-align:center;" colspan="2">
Edit Report Information
</td>
</tr>
<tr>
<td style="text-align:right;" width="40%">
Site Name:
</td>
<td width="60%">
#Html.EditorFor(model => item.rfds.location_name)
</td>
</tr>
<tr>
<td align="right">
FA Location Code:
</td>
<td>
#Html.EditorFor(model => item.rfds.fa_location_code)
</td>
</tr>
<tr>
<td align="right">
Site Address:
</td>
<td>
#Html.EditorFor(model => item.rfds.address)
</td>
</tr>
<tr>
<td align="right">
Site City:
</td>
<td>
#Html.EditorFor(model => item.rfds.city)
</td>
</tr>
<tr>
<td align="right">
Site State:
</td>
<td>
#Html.EditorFor(model => item.rfds.state)
</td>
</tr>
<tr>
<td align="right">
Site Zip Code:
</td>
<td>
#Html.EditorFor(model => item.rfds.zip_code)
</td>
</tr>
<tr>
<td colspan="2">
<hr />
</td>
</tr>
<tr>
<td align="right">
Sub Client:
</td>
<td>
#Html.EditorFor(model => item.subclient.sub_client)
</td>
</tr>
<tr>
<td align="right">
Sub Client Attn:
</td>
<td>
#Html.EditorFor(model => item.subclient.sub_client_attn)
</td>
</tr>
<tr>
<td align="right">
Sub Client Address:
</td>
<td>
#Html.EditorFor(model => item.subclient.sub_client_address)
</td>
</tr>
<tr>
<td align="right">
Sub Client City:
</td>
<td>
#Html.EditorFor(model => item.subclient.sub_client_city)
</td>
</tr>
<tr>
<td align="right">
Sub Client State:
</td>
<td>
#Html.EditorFor(model => item.subclient.sub_client_state)
</td>
</tr>
<tr>
<td align="right">
Sub Client Zip Code:
</td>
<td>
#Html.EditorFor(model => item.subclient.sub_client_zip)
</td>
</tr>
<tr>
<td colspan="2">
<hr />
</td>
</tr>
<tr>
<td align="right">
Capability %:
</td>
<td>
#Html.EditorFor(model => item.rfds.capability)
</td>
</tr>
<tr>
<td colspan="2">
<hr />
</td>
</tr>
<tr>
<td align="right">
Report Date:
</td>
<td>
#Html.EditorFor(model => item.rfds.submit_date)
</td>
</tr>
<tr>
<td align="right">
Job Number:
</td>
<td>
#Html.EditorFor(model => item.rfds.job_number)
</td>
</tr>
<tr>
<td align="right">
Revision:
</td>
<td>
#Html.EditorFor(model => item.rfds.revision)
</td>
</tr>
<tr>
<td colspan="2">
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Save" />
</td>
</tr>
</table>
</td>
</tr>
}
}
</table>
</div>

How to multiply two columns in a table together to produce a new column

I have 2 fields quantity and price. I basically want to multiply them together and get a value for another column called Price (Which is the total of the multiplication).
I have tried using the below html code:
#Html.DisplayFor(modelItem => item.item_order_quantity*item.ITEM.item_price)
This is my Table row code:
<table class="table table-striped table-advance table-hover">
<tbody>
<tr>
<th><i class="icon_pin_alt"></i> Item Description</th>
<th><i class="icon_pin_alt"></i> Quantity</th>
<th><i class="icon_calendar"></i> Price</th>
</tr>
#foreach (var item in Model.ITEM_ORDER)
{
<tr>
<td style="width:auto">
#Html.DisplayFor(modelItem => item.ITEM.item_description)
</td>
<td style="width:auto">
#Html.DisplayFor(modelItem => item.item_order_quantity)
</td>
<td style="width:auto">
#Html.DisplayFor(modelItem => item.item_order_quantity*item.ITEM.item_price)
</td>
<td>
<div class="btn-group">
#Html.ActionLink("View", "Details", new { id = item.OrderID }) |
#Html.ActionLink("Edit", "Edit", new { id = item.OrderID }) |
#Html.ActionLink("Delete", "Delete", new { id = item.OrderID })
</div>
</td>
</tr>
}
</tbody>
</table>
Consider having an additional property in your viewmodel or Model which does this task for you. Then you can bind this with html helper as you did for every other field.
public class YourViewModel
{
public int Field1{ get; set; }
public int Field2{ get; set; }
public int CalculatedField{
get {return Field1*Field2;}
}
}
Or try below code which calculates the value and stores in variable, then renders value directly from variable.
try this
<table class="table table-striped table-advance table-hover">
<tbody>
<tr>
<th><i class="icon_pin_alt"></i> Item Description</th>
<th><i class="icon_pin_alt"></i> Quantity</th>
<th><i class="icon_calendar"></i> Price</th>
</tr>
#foreach (var item in Model.ITEM_ORDER)
{
var computedValue = item.item_order_quantity*item.ITEM.item_price
<tr>
<td style="width:auto">
#Html.DisplayFor(modelItem => item.ITEM.item_description)
</td>
<td style="width:auto">
#Html.DisplayFor(modelItem => item.item_order_quantity)
</td>
<td style="width:auto">
#(computedValue)
</td>
<td>
<div class="btn-group">
#Html.ActionLink("View", "Details", new { id = item.OrderID }) |
#Html.ActionLink("Edit", "Edit", new { id = item.OrderID }) |
#Html.ActionLink("Delete", "Delete", new { id = item.OrderID })
</div>
</td>
</tr>
}
</tbody>
</table>

Issue Saving Edited Data: DDL Value can not be null

I am having an issue with saving the edited fields in my MVC project. When I click the submit button I get an error
Value cannot be null.
Parameter name: items
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: items
Source Error:
Line 63:
Line 64:
Line 65: #Html.DropDownListFor(model => model.EventTypeID, new SelectList(ViewBag.EventTypes, "EventTypeID", "EventType1"), "Choose")
Line 66:
Line 67:
I have tried some things that I have seen on the internet but it doesn't seem to be doing anything for me :( Help would be appreciated thank you!
this is my controller When I put a break point here it never triggers.
[HttpPost]
public ActionResult IndexPST(Event_Setup es)
{
db.Event_Setup.Add(es);
db.SaveChanges();
return View("Index",es);
}
And this is my View
#model DixonGroupInc.Models.Event_Setup
<link href="~/Content/EventManagement.css" rel="stylesheet" />
#using (Html.BeginForm("IndexPST", "EventManagement", FormMethod.Post))
{
#Html.AntiForgeryToken()
#Html.ValidationSummary(true)
<div id="cntr">
<div id="emLoc">
<table id="ldTbl">
<thead>
<tr>
<th class="ln">
<img src="~/Images/4.gif" />
</th>
<th>
<img src="~/Images/5.gif" />
</th>
<th>
<img src="~/Images/5.gif" />
</th>
<th>
<img src="~/Images/5.gif" />
</th>
<th>
<img src="~/Images/5.gif" />
</th>
</tr>
</thead>
<tbody>
<tr>
<td class="chosen space">EventMetadata
</td>
<td class="space">Client / Vendor
</td>
<td class="space">Venu info
</td>
<td class="space">Sponsored Participants
</td>
<td class="space">Event Services
</td>
</tr>
</tbody>
</table>
</div>
<div id="emSetup">
Setup Meeting
</div>
<div id="emTable">
<table class="myTbl">
<tr>
<td class="lblEvent clm1">
#Html.Label("Event Type")
</td>
<td class="clm2">
#Html.DropDownListFor(model => model.EventTypeID, new SelectList(ViewBag.EventTypes, "EventTypeID", "EventType1"), "Choose")
</td>
<td class="clm3">
#{Html.RenderAction("EventType", "EventManagement");}
</td>
</tr>
<tr class="rows">
<td class="lblEvent clm1">
#Html.Label("Event Title")
</td>
<td class="clm2">
#Html.EditorFor(model => model.EventTitle)
#Html.ValidationMessageFor(model => model.EventTitle)
</td>
</tr>
<tr>
<td class="lblEvent clm1">
#Html.Label("Event Identifier")
</td>
<td class="clm2">
#Html.EditorFor(model => model.EventIdentifier)
#Html.ValidationMessageFor(model => model.EventIdentifier)
</td>
</tr>
</table>
<table class="dateTbl">
<tr class="rows">
<td class="lblEvent clm1">
#Html.Label("Event Date From")
</td>
<td class="clm2">
#Html.TextBoxFor(model => model.EventDateFrom, new { #class = "dateFrom" })
#Html.ValidationMessageFor(model => model.EventDateFrom)
</td>
<td class="lblEvent">
#Html.Label("To")
</td>
<td class="clm4">
#Html.TextBoxFor(model => model.EventDateTo, new { #class = "dateTo" })
#Html.ValidationMessageFor(model => model.EventDateTo)
</td>
</tr>
</table>
<table class="myTbl">
<tr>
<td class="lblEvent clm1">
#Html.Label("Event Description")
</td>
<td class="clm2">
#Html.TextAreaFor(model => model.EventDescription, new
{
id = "taED"
})
<div>
<span id="charLeft2"></span>characters remaining.
</div>
#Html.ValidationMessageFor(model => model.EventDescription)
</td>
</tr>
<tr class="rows">
<td class="lblEvent clm1">
#Html.Label("Custom Message")
</td>
<td class="clm2">
#Html.TextAreaFor(model => model.CustomMessage, new
{
id = "taCM"
})
<div>
<span id="charLeft1"></span>characters remaining.
</div>
#Html.ValidationMessageFor(model => model.CustomMessage)
</td>
</tr>
<tr>
<td class="lblEvent clm1">
#Html.Label("Instructions")
</td>
<td class="clm2">
#Html.TextAreaFor(model => model.Instructions, new
{
id = "taInstrct"
})
<div>
<span id="charLeft"></span>characters remaining.
</div>
#Html.ValidationMessageFor(model => model.Instructions)
</td>
</tr>
</table>
<table class="myTbl">
<tr class="rows">
<td>
#{Html.RenderAction("AddTrack", "EventManagement");}
</td>
</tr>
<tr>
<td>
#{Html.RenderAction("EventTrack", "EventManagement");}
</td>
</tr>
</table>
<table class="myTbl">
<tr class="rows">
<td class="lblEvent clm1">
#Html.Label("CFP Pocess")
</td>
<td class="clm2">
#Html.RadioButtonFor(model => model.CFPRequired, "true") Yes
</td>
<td class="clm3">
#Html.RadioButtonFor(model => model.CFPRequired, "false") No
</td>
</tr>
</table>
</div>
<div id="emSubmit">
<input type="submit" value="Save & Next" id="submit" />
</div>
</div>
}
Viewbag can be unreliable so generally it isn't recommended to pass your drop down list data through the viewbag. I would recommend adding it to your view model instead. In your Event_Setup class make sure that you have
public int EventTypeID { get; set; }
and
public List<SelectListItem> EventTypeList { get; set; }
on your controller get you can set EventTypeID to set a default on the drop down so your controller should have something like
Event_Setup es = new Event_Setup();
es.EventTypeID = //default value if you want;
es.EventTypeList = //Method call here where List<SelectListItem> is returned with your event list
then on your view you can change your drop down to
#Html.DropDownListFor(x => x.EventTypeID, Model.EventTypeList)