I read about these warnings but I did not manage to solve the problem.
VS have last update.
TS1002 (JS) ',' expected.
TS1005 (JS) Unterminated string literal.
TS1002 (JS) ',' expected.
TS1005 (JS) Unterminated string literal.
these warnings were written 2x twice as I wrote (I don't understand what it means)
file type - ViewAll.cshtml
#model IEnumerable<WebSQLserver.Models.Employee>
#{
ViewBag.Title = "ViewAll";
Layout = null;
}
<table class="table table-striped table-condensed" id="employeeTable">
<thead>
<tr>
<th>
#Html.DisplayNameFor(model => model.Name)
</th>
<th>
#Html.DisplayNameFor(model => model.Position)
</th>
<th>
#Html.DisplayNameFor(model => model.Office)
</th>
<th>
#Html.DisplayNameFor(model => model.Salary)
</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>
<td>
#Html.DisplayFor(modelItem => item.Salary)
</td>
<td>
<a class="btn btn-primary btn-sm" onclick="Edit('#Url.Action("AddOrEdit", "Employee", new { id=#item.EmployeeID})')"><i class="fa fa-pencil fa-lg"></i></a>
<a class="btn btn-danger btn-sm" onclick="Delete('#Url.Action("Delete", "Employee", new { id=#item.EmployeeID})')"><i class="fa fa-trash fa-lg"></i></a>
</td>
</tr>
}
</tbody>
</table>
file type Controller.cs
namespace WebSQLserver.Controllers
{
public class EmployeeController : Controller
{
// GET: Employee
public ActionResult Index()
{
return View();
}
public ActionResult ViewAll()
{
return View(GetAllEmployee());
}
IEnumerable<Employee> GetAllEmployee()
{
using(DBModel db = new DBModel())
{
return db.Employees.ToList<Employee>();
}
}
Related
I have 2 filled ILists Suppliers and VwSrmAhmSuppliers with a common property Supplier.SupplierNo and VwSrmAhmSupplier.AhmSupplierNo. I'd like to combine them on that common property into one IList where the individual class properties are still accessible so I can efficiently access them in the View.
So if I pulled the first item in CommonList and then asked for CommonList(1).Supplier.SupplierNo and CommonList(1).VwAhmSrmSupplier.AhmSupplierNo - those 2 fields would be the same.
Maybe I have a class like this:
public class SupplierDetail
{
public Supplier Supplier { get; set; }
public Models.ExternalData.VwSrmAhmSupplier VwSrmAhmSupplier { get;set;}
}
public IList<Supplier> Suppliers { get;set; }
public IList<Models.ExternalData.VwSrmAhmSupplier> VwSrmAhmSuppliers { get; set; }
public IList<SupplierDetail> SupplierDetails;
public async Task OnGetAsync(Boolean? All)
{
//don't show all records unless explicity asked to!
if (All == true)
{
Suppliers = await _context.Supplier
.Include(s => s.Status)
.Include(c => c.Category)
.Include(c => c.Comments)
.OrderByDescending(c => c.CreateDate)
.ToListAsync();
var supplierNos = Suppliers.Select(s => s.SupplierNo).ToList();
VwSrmAhmSuppliers = await _externalcontext.VwSrmAhmSuppliers
.Where(v => supplierNos
.Any(s => s == v.AhmSupplierNo))
.ToListAsync();
SupplierDetails = ??
}
The HTML table I'm trying to generate would be something like this:
<tbody>
#foreach (var item in Model.CommonList)
{
<tr>
<td>
<a asp-page="./Details" asp-route-id="#item.Id">#Html.DisplayFor(modelItem => item.Supplier.SupplierNo)</a>
</td>
<td>
#Html.DisplayFor(modelItem => item.VwSrmAhmSupplier.AhmSupplierNm)
</td>
<td>
#Html.DisplayFor(modelItem => item.Supplier.CreateDate)
</td>
<td>
#Html.DisplayFor(modelItem => item.Supplier.Creator)
</td>
<td>
#Html.DisplayFor(modelItem => item.Supplier.Category.Description)
</td>
<td>
#Html.DisplayFor(modelItem => item.Supplier.Status.Description)
</td>
<td>
<a asp-page="./Delete" asp-route-id="#item.Id" class="btn btn-danger">Hide</a>
</td>
</tr>
}
</tbody>
Thanks #Selvin for the help in leading me down the right path.
Create a composite class of 2 classes
public class SupplierDetail
{
public Supplier Supplier { get; set; }
public VwSrmAhmSupplier VwSrmAhmSupplier { get;set;}
}
Then, fill the list by looping:
SupplierDetails = new List<SupplierDetail>();
foreach (Supplier supplier in Suppliers)
{
SupplierDetail supplierDetail = new SupplierDetail
{
Supplier = supplier,
VwSrmAhmSupplier = VwSrmAhmSuppliers.Where(s => s.AhmSupplierNo == supplier.SupplierNo).First()
};
SupplierDetails.Add(supplierDetail);
}
Finally, access your object properties in the view
<table id="table" class="table table-striped table-bordered" style="width:100%">
<thead>
<tr>
<th>
#Html.DisplayNameFor(model => model.SupplierDetails[0].Supplier.SupplierNo)
</th>
<th>
#Html.DisplayNameFor(model => model.SupplierDetails[0].VwSrmAhmSupplier.AhmSupplierNm)
</th>
<th>
#Html.DisplayNameFor(model => model.SupplierDetails[0].Supplier.CreateDate)
</th>
<th>
#Html.DisplayNameFor(model => model.SupplierDetails[0].Supplier.Creator)
</th>
<th>
#Html.DisplayNameFor(model => model.SupplierDetails[0].Supplier.Category)
</th>
<th>
#Html.DisplayNameFor(model => model.SupplierDetails[0].Supplier.Status)
</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach (var item in Model.SupplierDetails)
{
<tr>
<td>
<a asp-page="./Details" asp-route-id="#item.Supplier.Id">#Html.DisplayFor(modelItem => item.Supplier.SupplierNo)</a>
</td>
<td>
#Html.DisplayFor(modelItem => item.VwSrmAhmSupplier.AhmSupplierNm)
</td>
<td>
#Html.DisplayFor(modelItem => item.Supplier.CreateDate)
</td>
<td>
#Html.DisplayFor(modelItem => item.Supplier.Creator)
</td>
<td>
#Html.DisplayFor(modelItem => item.Supplier.Category.Description)
</td>
<td>
#Html.DisplayFor(modelItem => item.Supplier.Status.Description)
</td>
<td>
<a asp-page="./Delete" asp-route-id="#item.Supplier.Id" class="btn btn-danger">Hide</a>
</td>
</tr>
}
</tbody>
</table>
This is an ASP.NET MVC project. I have a view that displays a collection of BookRentViewModel items.
When I click on the button in the view, the view is posted to an action method called rent. rent action method has a parameter called items of type IEnumerable<BookRentViewModel>.
Here is the code of the view:
#model IEnumerable<DaramSerl.Models.ViewModels.BookRentViewModel>
#using (Html.BeginForm("rent", "RentBook", FormMethod.Post, new { #class = "form-horizontal", role = "form" }))
{
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.bookName)
</th>
<th>
#Html.DisplayNameFor(model => model.isRented)
</th>
<th>
#Html.DisplayNameFor(model => model.startDate)
</th>
<th>
#Html.DisplayNameFor(model => model.endDate)
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.EditorFor(modelItem => item.bookName)
</td>
<td>
#Html.EditorFor(modelItem => item.isRented)
</td>
<td>
#Html.EditorFor(modelItem => item.startDate)
</td>
<td>
#Html.EditorFor(modelItem => item.endDate)
</td>
</tr>
}
</table>
<input type="submit" value="Create" class="btn btn-primary" />
}
And here is the action method:
[HttpPost]
public async Task<ActionResult> rent(IEnumerable<BookRentViewModel> items)
{
if (ModelState.IsValid)
{
return RedirectToAction("/Index");
}
return View();
}
And here is the BookRentViewModel:
public class BookRentViewModel
{
[Key]
public int bookId{ get; set; }
public string bookName { get; set; }
public bool isRented { get; set; }
public DateTime startDate { get; set; }
public DateTime endDate { get; set; }
}
The problem is that items parameter is always null when rent action is triggered.
Any idea why items is null and does not get the collection from the view?
UPDATE
I used fiddler to see if the collection is posted to the server but it seems it cannot be parsed into BookRentViewModel items.
UPDATE3
Here is screenshot from fiddler with the collection sent from the view:
Doesnt look like you're setting a value for public int bookId - if thats the case this will fail validation as its a non-nullable type.
Either way, set a breakpoint in your controller method and check the errors on ModelState - see Get error message if ModelState.IsValid fails?
Edit
To include properties but not show them on your view, use the hidden field razor tag helper:
#Html.HiddenFor(x => x.bookId)
I tried reproduce source code.
You can change model mapping in cshtml file as below
#for (int i = 0; i < Model.Count();i++ )
{
<tr>
<td>#Html.TextBox("items[" + #i + "].bookName",
Model.ElementAt(i).bookName
)</td>
<td>#Html.CheckBox("items[" + #i + "].isRented",
Model.ElementAt(i).isRented
)</td>
<td>#Html.TextBox("items[" + #i + "].startDate",
Model.ElementAt(i).startDate
)</td>
<td>#Html.TextBox("items[" + #i + "].endDate",
Model.ElementAt(i).endDate
)</td>
</tr>
}
rent.cshtml file
#model IEnumerable<WebApplication2.Controllers.BookRentViewModel>
#using (Html.BeginForm("rent", "Rent", FormMethod.Post, new { #class = "form-horizontal", role = "form" }))
{
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.bookName)
</th>
<th>
#Html.DisplayNameFor(model => model.isRented)
</th>
<th>
#Html.DisplayNameFor(model => model.startDate)
</th>
<th>
#Html.DisplayNameFor(model => model.endDate)
</th>
</tr>
#*#foreach (var item in Model)
{
<tr>
<td>
#Html.EditorFor(modelItem => item.bookName)
</td>
<td>
#Html.EditorFor(modelItem => item.isRented)
</td>
<td>
#Html.EditorFor(modelItem => item.startDate)
</td>
<td>
#Html.EditorFor(modelItem => item.endDate)
</td>
</tr>
}*#
#for (int i = 0; i < Model.Count();i++ )
{
<tr>
<td>#Html.TextBox("items[" + #i + "].bookName",
Model.ElementAt(i).bookName
)</td>
<td>#Html.CheckBox("items[" + #i + "].isRented",
Model.ElementAt(i).isRented
)</td>
<td>#Html.TextBox("items[" + #i + "].startDate",
Model.ElementAt(i).startDate
)</td>
<td>#Html.TextBox("items[" + #i + "].endDate",
Model.ElementAt(i).endDate
)</td>
</tr>
}
</table>
<input type="submit" value="Create" class="btn btn-primary" />
}
I have actions which are accessible both by Authenticated and Guest User. So I have marked my controller and actions as
namespace FlightManagementV1.Controllers
{
[Authorize]
public class SearchFlightGuestController : Controller
{
// GET: SearchFlightGuest
[AllowAnonymous]
public ActionResult SearchFlightGuest()
{
return View();
}
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult SearchFlightGuest1(FlightScheduleModel obj)
{
CustomerHelper helper = new CustomerHelper();
List<FlightScheduleModel> list = helper.SearchFlightGuest(obj);
if (list.Count == 0)
{
TempData["ErrorMessage"] = "Sorry!!! We are unable to find any matching Flight for you.";
return RedirectToAction("SearchFlightGuest", "SearchFlightGuest");
}
return View(list);
}
}
}
The View SearchFlightGuest1 has a link which redirects to an action marked by [Authorize] attribute. In case of Guest Users link is automatically redirecting to the login page but in case of Authenticated users, the link is also redirecting to the login page and not accessing action marked by [Authorize] attribute.
View SearchFlightGuest1
<h3>We have found following flights for you</h3>
<br />
<table border="1" style="background-color : white; font-weight : normal">
<tr>
<th>
#Html.Label("Flight ID")
</th>
<th>
#Html.Label("Departure Place")
</th>
<th>
#Html.Label("Destination Place")
</th>
<th>
#Html.Label("Departure Date and Time")
</th>
<th>
#Html.Label("Arrival Date and Time")
</th>
<th>
#Html.Label("Premium Fare (Rs)")
</th>
<th>
#Html.Label("First Fare (Rs)")
</th>
<th>
#Html.Label("Economy Fare (Rs)")
</th>
<th>
#Html.Label("Available (Premium)")
</th>
<th>
#Html.Label("Available (First)")
</th>
<th>
#Html.Label("Available (Economy)")
</th>
<th>
#Html.Label("Distance (KM)")
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.FlightId)
</td>
<td class="field">
#Html.DisplayFor(modelItem => item.DepPlace)
</td>
<td class="field">
#Html.DisplayFor(modelItem => item.DestPlace)
</td>
<td class="field-datetime">
#Html.DisplayFor(modelItem => item.DepartureDT)
</td>
<td class="field-datetime">
#Html.DisplayFor(modelItem => item.ArrivalDT)
</td>
<td class="field">
#Html.DisplayFor(modelItem => item.FarePremium)
</td>
<td class="field">
#Html.DisplayFor(modelItem => item.FareFirst)
</td>
<td class="field">
#Html.DisplayFor(modelItem => item.FareEconomy)
</td>
<td class="field">
#Html.DisplayFor(modelItem => item.AvailablePremium)
</td>
<td class="field">
#Html.DisplayFor(modelItem => item.AvailableFirst)
</td>
<td class="field">
#Html.DisplayFor(modelItem => item.AvailableEconomy)
</td>
<td class="field">
#Html.DisplayFor(modelItem => item.Distance)
</td>
<td>
#Html.ActionLink("Book Journey", "BookingDetails", "BookJourney", item, new { })
</td>
</tr>
}
</table>
Action i am trying to access
public class BookJourneyController : Controller
{
// GET: BookFlight
[Authorize]
public ActionResult BookingDetails(FlightScheduleModel schedule)
{
FlightJourneyViewModel journeyViewModel = new FlightJourneyViewModel();
journeyViewModel.flightSchedule = schedule;
Session["flight_id"] = schedule.FlightId;
return View(journeyViewModel);
}
}
I'm trying to submit only these rows which is checked. I'm using this jquery plungin and my table looks same as is in link
#model IEnumerable<BillBox.ACD_UNI_STUDENTS>
<table class="table tblSelect">
<tr>
<th>
<input type="checkbox" id="checkall" title="Select all" />
</th>
<th>
#Html.DisplayNameFor(model => model.FIRST_NAME)
</th>
<th>
#Html.DisplayNameFor(model => model.LAST_NAME)
</th>
<th>
#Html.DisplayNameFor(model => model.PERSONAL_NUMBER)
</th>
<th>
#Html.DisplayNameFor(model => model.ACD_UNI_DEGREES.DEGREE)
</th>
<th>
#Html.DisplayNameFor(model => model.ACD_UNI_FACULTIES.FACULTY)
</th>
<th>
#Html.DisplayNameFor(model => model.ACD_UNI_SEMESTERS.SEMESTER)
</th>
<th>
#Html.DisplayNameFor(model => model.ACD_UNI_SPECIALIZATIONS.SPECIALIZATION)
</th>
<th>
#Html.DisplayNameFor(model => model.COR_PAYER_STATUS.NAME)
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
<input type="checkbox"/>
</td>
<td>
#Html.DisplayFor(modelItem => item.FIRST_NAME)
</td>
<td>
#Html.DisplayFor(modelItem => item.LAST_NAME)
</td>
<td>
#Html.DisplayFor(modelItem => item.PERSONAL_NUMBER)
</td>
<td>
#Html.DisplayFor(modelItem => item.ACD_UNI_FACULTIES.FACULTY)
</td>
<td>
#Html.DisplayFor(modelItem => item.ACD_UNI_SEMESTERS.SEMESTER)
</td>
<td>
#Html.DisplayFor(modelItem => item.ACD_UNI_SEMESTERS.SEMESTER)
</td>
<td>
#Html.DisplayFor(modelItem => item.ACD_UNI_SPECIALIZATIONS.SPECIALIZATION)
</td>
<td>
#Html.DisplayFor(modelItem => item.COR_PAYER_STATUS.NAME)
</td>
</tr>
}
</table>
Now I have a question. How can I retrieve only these rows which checkbox is checked?
that's my controller
public PartialViewResult AllStudent()
{
var students = (from q in db.ACD_UNI_STUDENTS
select q).ToList();
return PartialView(students);
}
there are many rows (from db) so I can't do it with formcollection. I can't retrieve their names
You may submit the selected ids as IEnumerable and then filter them in your controller! Here is an example
<form action="url" method="post">
...
#foreach (var item in Model)
{
<tr>
<td>
<input type="checkbox" name="Objs[]" id="Objs[]" value="#item.UNIQUE_ID"/>
</td>
<td>
#Html.DisplayFor(modelItem => item.FIRST_NAME)
</td>
</tr>
}...
</form>
and your controller
[HttpPost]
public PartialViewResult AllStudent(IEnumerable<long> Objs)
{
var students = (from q in db.ACD_UNI_STUDENTS
where Objs.Contains(q.UNIQUE_ID)
select q).ToList();
return PartialView(students);
}
...
I'd suggest not to use the Database Model for the View directly. You should have a Model to bind to the Rows, which might look like:
public class StudentRowViewModel
{
public bool IsChecked { get; set; }
public int StudentId { get; set; }
// ... More columns, whatever you want to display
public string Name { get; set; }
}
When populating the View, you select StudentRowViewModels like this:
db.ACD_UNI_STUDENTS.Select(x => new StudentRowViewModel { StudentId = x.UNIQUE_ID, Name = ... });
Or whatever applies to your DataBase Model.
In your View, your Checkbox will also Bind to the Model:
#Html.CheckboxFor(x => x.IsChecked)
Finally, when the Form is submitted, you can select only the checked Items:
public ActionResult AllStudents(IEnumerable<StudentRowViewModel> model)
{
var checked = model.Where(x => x.IsChecked).Select(x => x.StudentId).ToList();
var items = db.ACD_UNI_STUDENTS.Where(x => checked.Contains(x.UNIQUE_ID));
}
You get the Idea?
First put a class and value attribute in your checkbox :
<input type="checkbox" class="myCheckBox" value="#item.PERSONAL_NUMBER" />
Then i advise you to use a JQuery script to detect which checkbox are checked :
I suppose that you have a button to do another action :
$(document).ready(function () {
$('#myButton').click(function() {
var id = '';
$('.myCheckBox:checked').each(function (e) {
id += $(this).val() + ';'
}
var url = '/ControllerName/ActionResultName';
$.ajax({
url:url,
cache: false,
type: 'POST',
data: {
Id: id
},
succes: function(data){
$('.myCheckBox').attr('checked',false);
location.reload();
}
})
}
});
After that you can get in your controller all the personnal numbers in your controller :
put a string parameter 'id' to your actionresult.
id.TrimEnd(';')
I have a table with data, how can I populate a form on the same page with the data when the edit button is clicked. Basically is should be the same as this example but without using knockoutjs
http://jsfiddle.net/jiggle/2cr2f/
#model IEnumerable<GenomindApp2.Areas.RulesEngine.ViewModels.GeneViewModel>
#{
ViewBag.Title = "Index2";
}
<table>
<tr>
<th>
#Html.DisplayNameFor(model => model.GeneValue)
</th>
<th>
#Html.DisplayNameFor(model => model.GeneCode)
</th>
<th>
#Html.DisplayNameFor(model => model.GeneName)
</th>
<th>
#Html.DisplayNameFor(model => model.GeneComments)
</th>
<th>
#Html.DisplayNameFor(model => model.WildType)
</th>
<th>
#Html.DisplayNameFor(model => model.WildTypeAllele)
</th>
<th>
#Html.DisplayNameFor(model => model.AtRiskAllele)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.GeneCode)
</td>
<td>
#Html.DisplayFor(modelItem => item.GeneName)
</td>
<td>
#Html.DisplayFor(modelItem => item.GeneComments)
</td>
<td>
#Html.DisplayFor(modelItem => item.WildType)
</td>
<td>
#Html.DisplayFor(modelItem => item.WildTypeAllele)
</td>
<td>
#Html.DisplayFor(modelItem => item.AtRiskAllele)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { }) |
#Html.ActionLink("Details", "Details", new { }) |
#Html.ActionLink("Delete", "Delete", new { })
</td>
</tr>
}
</table>
You should do it like that:
Model:
public class ModelB
{
public int Age { get; set; }
public string Name { get; set; }
}
Controller:
public ActionResult MyAction()
{
var model = new List<ModelB>
{
new ModelB{Age = 2, Name = "Bob"},
new ModelB{Age = 7, Name = "Sam"},
};
return View(model);
}
[HttpPost]
public ActionResult MyAction(List<ModelB> model)
{
//whatever
}
View:
#model List<TestWebApplication.Models.ModelB>
...
#using (Html.BeginForm())
{
for (int i = 0; i < Model.Count; i++)
{
Age: #Html.EditorFor(modelItem => Model[i].Age);
Name: #Html.EditorFor(modelItem => Model[i].Name);
<br />
}
<input type="submit"/>
}
Please note that I used for instead of foreach. When you populate a form you shouldn't use foreach - it will not render well.