API - Blazor server - foreign key ICollection is always null - EF core - api

I'm new to API and Blazor and I'm trying to follow this example (https://learn.microsoft.com/en-us/aspnet/core/data/ef-rp/crud?view=aspnetcore-5.0)
Below you can see my models and code.
Model:
public class Student {
public int StudentId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime EnrollmentDate { get; set; }
[JsonIgnore]
public virtual ICollection<Enrollment> Enrollments { get; set;}
}
API controller:
[HttpGet]
[Route("{id:int}")]
public async Task<ActionResult<Student>> GetStudent(int id)
{
try
{
var result = await context.Students
.Include(s => s.Enrollments)
.ThenInclude(e => e.Course)
.AsNoTracking()
.FirstOrDefaultAsync(m => m.StudentId == id);
//var result = await context.Students
// .Where(s => s.StudentId == id)
// .Select(s => new
// {
// Student = s,
// Enrollment = s.Enrollments
// })
// .FirstOrDefaultAsync();
if (result == null)
{
return NotFound();
}
return Ok(result);
}
catch (Exception)
{
return StatusCode(StatusCodes.Status500InternalServerError, "Error receiving data from database");
}
}
Student model in Blazor server
public class Student
{
public int StudentId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime EnrollmentDate { get; set; }
[JsonIgnore]
public virtual ICollection<Enrollment> Enrollments { get; set; }
}
My Blazor Student base:
public class StudentDetailsBase : ComponentBase
{
[Inject]
public IEnrollmentService EnrollmentService { get; set; }
[Inject]
public IStudentService StudentService { get; set; }
public Student Student { get; set; }
public List<Enrollment> Enrollments { get; set; }
//public ICollection<Student> Student { get; set; }
[Parameter]
public string Id { get; set; }
protected override async Task OnInitializedAsync()
{
Id = Id ?? "1";
Student = await StudentService.GetStudent(int.Parse(Id));
Enrollments = (await EnrollmentService.GetEnrollmentBySID(int.Parse(Id))).ToList();
//Student = (await StudentService.GetStudent(int.Parse(Id))).ToList();
}
}
And my Student display page:
#if (Student == null)
{
<p>Loading ...</p>
}
else
{
<div>
<table class="table">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Enrollment Date</th>
<th>All enrollments</th>
</tr>
</thead>
<tbody>
<tr>
<td>#Student.FirstName</td>
<td>#Student.LastName</td>
<td>#Student.EnrollmentDate</td>
#foreach (var i in Student.Enrollments)
{
<td>#i.Course.Title</td>
<td>#i.Grade</td>
}
</tr>
</tbody>
</table>
</div>
}
I've tried to google the problem and i can not figure out what i'm doing wrong. My Student.Enrollments in always null. Which causes my Blazor server to throw an error.
When i test my API with Postman it's working fine.
Hopefully someone will point me in the right direction on how to solve this.
Thank you.
Kind regards.

Related

How can I display data from multiple entities in same view and group them by one of the entity

I have clients, projects, client comments and project comments. I want to display one table grouped by client followed by all projects and for each client where there is a comment as well as for each project that has a comment display the last provided comment.
The table would have the Client Name at the top followed by the latest respective comment if provided.
It would be followed by the list of all projects for that client with their latest comment if provided.
I have the client model:
public class Client
{
public int Id { get; set; }
public string ClientName { get; set; }
public bool IsActive { get; set; }
public ICollection<ClientComment> ClientComments { get; set; }
public ICollection<Project> Projects { get; set; }
The project model:
public class Project
{
public int Id { get; set; }
public string ProjectName { get; set; }
public int ClientId { get; set; }
public Client Client { get; set; }
public bool IsArchived { get; set; }
public ICollection<ProjectComment> ProjectComments { get; set; }
The client comment model:
public class ClientComment
{
public int Id { get; set; }
public int? ClientId { get; set; }
public Client Client { get; set; }
public string StatusComment { get; set; }
public DateTime LastUpdateDate { get; set; }
public ClientComment ()
{
this.LastUpdateDate = DateTime.UtcNow;
}
The project comment model:
public class ProjectComment
{
public int Id { get; set; }
public int? ProjectId { get; set; }
public Project Project { get; set; }
public string StatusComment { get; set; }
public DateTime LastUpdateDate { get; set; }
public ProjectComment ()
{
this.LastUpdateDate = DateTime.UtcNow;
}
The end result should be with their respective table headers:
ClientName1 | ClientStatusComment
ProjectName1 | ProjectStatusComment
ProjectName2 | ProjectStatusComment
ProjectName3 | ProjectStatusComment
ClientName2 | ClientStatusComment
ProjectName1 | ProjectStatusComment
ProjectName2 | ProjectStatusComment
ProjectName3 | ProjectStatusComment
You could use View Model which contains the properties you need display in the view.Refer to as follows:
ClientVM and ProjectVM
public class ClientVM
{
public string ClientName { get; set; }
public string ClientStatusComment { get; set; }
public List<ProjectVM> Projectlist { get; set; }
}
public class ProjectVM
{
public string ProjectName { get; set; }
public string ProjectStatusComment { get; set; }
}
Populate the ViewModel
public class ClientsDetailsModel : PageModel
{
private readonly MyDbContext _context;
public ClientsDetailsModel(MyDbContext context)
{
_context = context;
}
[BindProperty]
public List<ClientVM> clientVMList { get; set; }
public async Task<IActionResult> OnGet()
{
var clientlist = _context.Clients
.Include(c => c.ClientComments)
.Include(c => c.Projects)
.ThenInclude(p => p.ProjectComments).ToList();
clientVMList = new List<ClientVM>();
foreach (var item in clientlist)
{
ClientVM clientVM = new ClientVM()
{
Projectlist = new List<ProjectVM>()
};
clientVM.ClientName = item.ClientName;
if (item.ClientComments != null && item.ClientComments.Any())
{
clientVM.ClientStatusComment = item.ClientComments.OrderByDescending(cc => cc.LastUpdateDate).First().StatusComment;
}
else
{
clientVM.ClientStatusComment = "No StatusComment";
}
foreach (var projectItem in item.Projects)
{
ProjectVM projectVM = new ProjectVM();
projectVM.ProjectName = projectItem.ProjectName;
if (projectItem.ProjectComments != null && projectItem.ProjectComments.Any())
{
projectVM.ProjectStatusComment = projectItem.ProjectComments.OrderByDescending(pc => pc.LastUpdateDate).First().StatusComment;
}
else
{
projectVM.ProjectStatusComment = "No StatusComment";
}
clientVM.Projectlist.Add(projectVM);
}
clientVMList.Add(clientVM);
}
return Page();
}
}
ClientsDetails.cshtml
#page
#model MultipleEntitiesInSameView.Pages.ClientsDetailsModel
<table class="table">
<thead>
<tr>
<th>
Name
</th>
<th>
LastStatusComment
</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach (var item in Model.clientVMList)
{
<tr style="background-color:aliceblue;">
<td>
#Html.DisplayFor(modelItem => item.ClientName)
</td>
<td>
#Html.DisplayFor(modelItem => item.ClientStatusComment)
</td>
</tr>
#foreach (var projectItem in item.Projectlist)
{
<tr>
<td>
#Html.DisplayFor(modelItem => projectItem.ProjectName)
</td>
<td>
#Html.DisplayFor(modelItem => projectItem.ProjectStatusComment)
</td>
</tr>
}
}
</tbody>
</table>
4.Result :

How to deal with this decimal error for a price?

I'm working on this app that should show on "localhost/catalog" some data. I have a library for the models and for the services that the application might use. I am getting this error:
InvalidOperationException: The property 'Price' is not a navigation
property of entity type 'StoreAsset'. The 'Include(string)' method can
only be used with a '.' separated list of navigation property names.Microsoft.EntityFrameworkCore.Query.Internal.IncludeCompiler.WalkNavigations(IEntityType entityType, IReadOnlyList<string> navigationPropertyPaths, IncludeLoadTree includeLoadTree, bool shouldThrow)
Here is the code that I'm using (controller, models and view) and the service methods on bottom:
public class CatalogController : Controller
{
private IStoreAsset _assets;
public CatalogController(IStoreAsset assets)
{
_assets = assets;
}
public ActionResult Index()
{
var assetModels = _assets.GetAll();
var listingResult = assetModels
.Select(result => new AssetIndexListingModel
{
Id = result.Id,
Tipology = _assets.GetTipology(result.Id),
Size = _assets.GetSize(result.Id),
Price = decimal.Parse(_assets.GetPrice(result.Id))
});
var model = new AssetIndexModel()
{
Assets = listingResult
};
return View(model);
}
public class AssetIndexListingModel
{
public int Id { get; set; }
public string Size { get; set; }
public decimal Price { get; set; }
public string Tipology { get; set; }
public string ImageUrl { get; set; }
}
public abstract class StoreAsset
{
public int Id { get; set; }
[Required]
public Status Status { get; set; }
[Required]
public decimal Price { get; set; }
public string ImageUrl { get; set; }
}
public class Dress : StoreAsset
{
[Required]
public string Color { get; set; }
[Required]
public string Tipology { get; set; }
[Required]
public string Size { get; set; }
}
#model Models.Catalog.AssetIndexModel
<div id="assets">
<h3></h3>
<div id="assetsTable">
<table class="table table-condensed" id="catalogIndexTable">
<thead>
<tr>
<th>Size</th>
<th>Price</th>
<th>Tipology</th>
</tr>
</thead>
<tbody>
#foreach (var asset in Model.Assets)
{
<tr class="assetRow">
<td class="">
<a asp-controller="Catalog" asp-action="Detail" asp-route-id="#asset.Id">
<img src="#asset.ImageUrl" class="imageCell" />
</a>
</td>
<td class="">#asset.Price</td>
<td class="">#asset.Size</td>
<td class="">#asset.Tipology</td>
</tr>
}
</tbody>
</table>
</div>
public class StoreAssetService : IStoreAsset
{
private Context _context;
public StoreAssetService(Context context)
{
_context = context;
}
public void Add(StoreAsset newAsset)
{
_context.Add(newAsset);
_context.SaveChanges();
}
public IEnumerable<StoreAsset> GetAll()
{
return _context.StoreAssets
.Include(asset => asset.Status)
.Include(asset => asset.Price);
}
public StoreAsset GetById(int id)
{
// Return a query (same as returning GetAll().FirstOrDefault(...))
return _context.StoreAssets
.Include(assets => assets.Status)
.Include(assets => assets.Price)
// So it can return null with no problem
.FirstOrDefault(asset => asset.Id == id);
}
public StoreBranch GetCurrentLocation(int id)
{
throw new NotImplementedException();
}
// To implement and test
public string GetPrice(int id)
{
return _context.Dresses.FirstOrDefault(p => p.Id == id).Price.ToString();
}
public string GetSize(int id)
{
return _context.Dresses.FirstOrDefault(s => s.Id == id).Size;
}
public string GetStatus(int id)
{
throw new NotImplementedException();
}
public string GetTipology(int id)
{
var dress = _context.StoreAssets.OfType<Dress>()
.Where(b => b.Id == id);
// For now return other if it's not a party dress
return dress.Any() ? "Party" : "Other";
}
}
Should I use some ForeignKey attribute or change Price to a string?
Any help would be great thanks
As pointed out in the error message, the Include is for the Navigation property only.
You need to change below:
return _context.StoreAssets
.Include(asset => asset.Status)
.Include(asset => asset.Price);
To:
return _context.StoreAssets
.Include(asset => asset.Status).ToList();
Reference: https://learn.microsoft.com/en-us/ef/core/modeling/relationships#definition-of-terms
https://learn.microsoft.com/en-us/ef/core/querying/related-data
I am having yet another problem. When I go to "localhost/catalog" the page should display all columns/entries that I have in the database but it only displays one column. Is there something wrong in the foreach cicle?

High Entity Framework Core execution times

I've built a simple Asp.Net Core MVC app for use at work displaying data from a view in our MSSQL database. When querying this view from SSMS, execution time is ~100ms on average. When the same query is executed within my app, execution time is anywhere from ~800ms to ~1.5s.
Here is the LINQ from the controller:
public IActionResult Index()
{
var query =
from p in _context.vWebQuery
where p.Almachine == "600L"
orderby p.Aldatsta
select p;
return View(query);
}
Here is the Entity Class:
namespace BetaKestrel2.Models
{
public class vWebQuery
{
public double Wruntim { get; set; }
public short Wper { get; set; }
public double Quantity { get; set; }
[Column("Total Op TIme")]
public double? TotalTime { get; set; }
public string Alwon { get; set; }
public short Alopnum { get; set; }
public string Almachine { get; set; }
public double Alpersta { get; set; }
[DisplayFormat(DataFormatString = "{0:F2}")]
public double Allen { get; set; }
public short Alprevop { get; set; }
[DisplayFormat(DataFormatString = "{0:d}")]
public DateTime Aldatsta { get; set; }
public string Altimsta { get; set; }
public string Alstatus { get; set; }
public short Alperno { get; set; }
public string Macid { get; set; }
public string Macdesc { get; set; }
public string Partid { get; set; }
public string Partrevisionid { get; set; }
public string Routingmethod { get; set; }
public double Wqleft { get; set; }
public string Wstate { get; set; }
public string Wdesc { get; set; }
public string Partdesc { get; set; }
public string Toolid { get; set; }
public string Childpartid { get; set; }
public string msection { get; set; }
}
}
And the DbContext:(using .Net Core 3.0 for the .HasNoKey())
public partial class EfacDBContext : DbContext
{
public EfacDBContext()
{
}
public EfacDBContext(DbContextOptions<EfacDBContext> options)
: base(options)
{
}
public DbSet<vWebQuery> vWebQuery { get; set; }
public DbSet<vGRN> vGRN { get; set; }
public DbSet<vQuotationTracker> vQuotationTracker { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<vWebQuery>(entity =>
{
entity.HasNoKey();
entity.ToTable("vwebquery");
});
And an example .cshtml View:
#model IEnumerable<BetaKestrel2.Models.vWebQuery>
#{
ViewData["Title"] = "600L";
string highlight = "";
}
<h1>Work Centre Plan - #ViewData["Title"]</h1>
<table class="table table-sm table-bordered">
<thead>
<tr>
<th>Works Order</th>
<th>Part Number</th>
<th>Description</th>
<th>Op Number</th>
<th>Quantity</th>
<th>Latest Start Date</th>
<th>Previous Op</th>
<th>Total Op Time (mins)</th>
<th>Status</th>
<th>Qty Left</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
#if (item.Wstate == "COMP")
{
highlight = "background-color: green;";
}
else if (item.Alprevop == 0)
{
highlight = "background-color: yellow;";
}
else
{
highlight = "";
}
<tr style="#highlight">
<td>#Html.DisplayFor(modelItem => item.Alwon)</td>
<td>#Html.DisplayFor(modelItem => item.Partid)</td>
<td>#Html.DisplayFor(modelItem => item.Partdesc)</td>
<td>#Html.DisplayFor(modelItem => item.Alopnum)</td>
<td>#Html.DisplayFor(modelItem => item.Quantity)</td>
<td>#Html.DisplayFor(modelItem => item.Aldatsta)</td>
<td>#Html.DisplayFor(modelItem => item.Alprevop)</td>
<td>#Html.DisplayFor(modelItem => item.TotalTime)</td>
<td>#Html.DisplayFor(modelItem => item.Wstate)</td>
<td>#Html.DisplayFor(modelItem => item.Wqleft)</td>
</tr>
}
</tbody>
</table>
EF Core converts to the following SQL
SELECT
[v].[Aldatsta]
,[v].[Allen]
,[v].[Almachine]
,[v].[Alopnum]
,[v].[Alperno]
,[v].[Alpersta]
,[v].[Alprevop]
,[v].[Alstatus]
,[v].[Altimsta]
,[v].[Alwon]
,[v].[Childpartid]
,[v].[Macdesc]
,[v].[Macid]
,[v].[Partdesc]
,[v].[Partid]
,[v].[Partrevisionid]
,[v].[Quantity]
,[v].[Routingmethod]
,[v].[Toolid]
,[v].[Total Op TIme]
,[v].[Wdesc]
,[v].[Wper]
,[v].[Wqleft]
,[v].[Wruntim]
,[v].[Wstate]
,[v].[msection]
FROM [vwebquery] AS [v]
WHERE
[v].[Almachine] = N'BENDD'
AND [v].[Almachine] IS NOT NULL
ORDER BY
[v].[Aldatsta]
Results in:
info: Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor[4]
Executed ViewResult - view Index executed in 1540.6805000000002ms.
info: Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker[2]
Executed action BetaKestrel2.Controllers.BenddController.Index (BetaKestrel2) in 1541.4348ms
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1]
Executed endpoint 'BetaKestrel2.Controllers.BenddController.Index (BetaKestrel2)'
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
Request finished in 1541.8467ms 200 text/html; charset=utf-8
Whereas, in SSMS
Total execution time 124ms
I tried the AsNoTracking() and that didn't make a difference. Your last comment got me curious. 291 rows are returned from the query so I tried .Take(5) and execution time went down to 24ms. Could it literally just be the iteration of my foreach loop in the view that is taking up all the time?
When querying this view from SSMS, execution time is ~100ms on
average. When the same query is executed within my app, execution time
is anywhere from ~800ms to ~1.5s.
SQL Server Management Studio (SSMS)
RAW SQL NO OVER HEAD
EF Entityfamework
put this
var query =
from p in _context.vWebQuery
where p.Almachine == "BENDD"
orderby p.Aldatsta
select p;
in a block which times it, loop it 3 times and take the last time.
--create dbContext here. (_context)
--start loop (run 3 times)
--start timer
var query = (
from p in _context.vWebQuery
where p.Almachine == "BENDD"
orderby p.Aldatsta
select p
).Tolist();
--end timer -this is what you want to compare after the 3 run.
- yes it will be slower but you could make as non tracking
- should be a must fairer comparison.
-- end loop
tip - put this in debug, put break-point on index... change your query to ToList(), so that it executes the query at that point and not wen its in the view section.
Code from test: Modified
for (int i = 0; i <= 3; i++)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
var query = (from p in _context.vWebQuery
where p.Almachine == "600L"
orderby p.Aldatsta
select p
).ToList();
stopwatch.Stop();
Console.WriteLine(stopwatch.ElapsedMilliseconds);
}
.....

search function in ASP.NET MVC not working properly

i have a student table in my database that i created and i have a view that displays a list of all the students grouped by class... on top of the view i made a textbox and a search button to be able to access the student information faster. The problem is that i when i enter the first name and the last name in the textbox, nothing comes up. When i enter only the first name or only the last name, then it finds it. I'm new to programming and i can't figure out how to make it work. I would really appreciate if someone can help me with this. This is part of my code:
[HttpGet]
public ActionResult ViewStudents()
{
ViewBag.classes = db.Courses.ToList();
var studentCourses = db.StudentCourses.OrderBy(s=>s.Person.FirstName).ToList();
return View(studentCourses);
}
[HttpPost]
public ActionResult ViewStudents(string SearchString)
{
var student=new List<int>();
List<StudentCourse>sc=new List<StudentCourse>();
ViewBag.classes = db.Courses.ToList();
var studentCourse=db.StudentCourses.ToList();
var studentCourses = db.StudentCourses.OrderBy(s => s.Person.FirstName).ToList();
var substring = SearchString.IndexOf(" ").ToString();
if (!string.IsNullOrEmpty(SearchString))
{
student = (from p in db.People
where (p.FirstName.Contains(SearchString)) && (p.LastName.Contains(substring))||((p.FirstName.Contains(SearchString)) || (p.LastName.Contains(SearchString)))
select p.PersonId).ToList();
}
foreach (var s in studentCourse)
{
foreach (var i in student)
{
if (s.StudentId == i)
{
sc.Add(s);
}
}
}
return View(sc);
}
This is my view:
#model List<SchoolFinalProject.Models.StudentCourse>
#using (Html.BeginForm())
{
<div style="font-size:16px;"> <input type="text" id="search" placeholder="search" Name="SearchString" /><span class="glyphicon glyphicon-search"></span>
<input type="submit" value="search"></div>
}
#{
List<int> c = new List<int>();
foreach (var courses in ViewBag.classes)
{
foreach(var s in Model)
{
if(courses.CourseId==s.CourseId)
{
c.Add(courses.CourseId);
}
}
}
}
#foreach (var course in ViewBag.classes)
{
if(c.Contains(course.CourseId))
{
<h2>#course.Name<span>-</span>#course.Gender</h2>
<table class="table table-hover table-bordered table-striped">
<tr><th>First Name</th><th>Last Name</th><th>Email</th><th>Phone Number</th><th>Address</th><th>Date Of Birth</th></tr>
#foreach (var s in Model)
{
if(course.CourseId==s.CourseId)
{
<tr>
<td>#s.Person1.FirstName</td>
<td>#s.Person1.LastName</td>
<td>#s.Person1.Email</td>
<td>#s.Person1.PhoneNumber</td>
<td>#s.Person1.Address</td>
<td>#s.Person1.DateOfBirth</td>
<td>
<span class="glyphicon glyphicon-edit"></span>
#Html.ActionLink("Edit", "Edit","Person", new { id = s.Person1.PersonId }, null) |
<span class="glyphicon glyphicon-trash"></span>
#Html.ActionLink("Details", "Details","Person", new { id = s.Person1.PersonId }, null)
</td>
</tr>
}
}
</table>
}
}
Go to top of page
this is my person Model:
public partial class Person
{
public Person()
{
this.Bonus = new HashSet<Bonu>();
this.ConversationHistories = new HashSet<ConversationHistory>();
this.ConversationHistories1 = new HashSet<ConversationHistory>();
this.EmployeePaymentDetails = new HashSet<EmployeePaymentDetail>();
this.StudentCourses = new HashSet<StudentCourse>();
this.StudentCourses1 = new HashSet<StudentCourse>();
this.TeacherCourses = new HashSet<TeacherCourse>();
this.Reminders = new HashSet<Reminder>();
}
public int PersonId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string UserName { get; set; }
public string Email { get; set; }
public string PhoneNumber { get; set; }
public string Address { get; set; }
public Nullable<System.DateTime> DateOfBirth { get; set; }
public PersonType PersonTypeId { get; set; }
public Nullable<System.DateTime> LastModified { get; set; }
public Nullable<int> Gender { get; set; }
public Nullable<int> Status { get; set; }
public string FullName
{
get { return FirstName + ", " + LastName; }
}
public virtual ICollection<Bonu> Bonus { get; set; }
public virtual ICollection<ConversationHistory> ConversationHistories { get; set; }
public virtual ICollection<ConversationHistory> ConversationHistories1 { get; set; }
public virtual ICollection<EmployeePaymentDetail> EmployeePaymentDetails { get; set; }
public virtual ICollection<StudentCourse> StudentCourses { get; set; }
public virtual ICollection<StudentCourse> StudentCourses1 { get; set; }
public virtual ICollection<TeacherCourse> TeacherCourses { get; set; }
public virtual ICollection<Reminder> Reminders { get; set; }
}
}
You might want to try concatenating the first and last name properties in your person model like this:
[Display(Name = "Full Name")]
public string FullName
{
get
{
return LastName + ", " + FirstMidName;
}
}
There is a very good tutorial on what you are trying to do here: https://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-a-more-complex-data-model-for-an-asp-net-mvc-application
Also see this page of same tutorial: https://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application
As an aside, you might want to check out using the Datatables plugin, which gives you search functionality without have to query your database with each search: https://datatables.net

create CheckboxFor from List in View Model

My View Model Class is
public class StudentQuestions
{
public int StudentId{ get; set; }
public int FormId { get; set; }
public virtual ICollection<Questions> Question { get; set; }
}
and question class is
public partial class Questions
{
public int questionID { get; set; }
public string field_name { get; set; }
public string question { get; set; }
public int qutyp_refID {get,set}
public string description { get; set; }
public int ord { get; set; }
public bool IsEnabled { get; set;}
public virtual ICollection<Answers> Answers { get; set; }
}
in my view
#model Test.ViewModels.StudentQuestions
<table>
<tr><td>#Model.FormId</td><td>#Model.StudentId</td></tr>
#foreach(var q in Model.Question)
{
<tr>
<td> #Html.CheckBoxForFor(i=> i.Question.question)</td>
</tr>
}
</table>
I cant access i.Question.question but I can access in CheckBox, TextBox like following and I want to change Textbox to TextBoxFor and CheckBox to CheckBoxFor and TextBox to TextBoxFor
#foreach(var q in Model.Question)
{
<tr>
#if (#q.qutyp_refID == 4)
{
<td>#Html.CheckBox(q.questionID.ToString())
</td>
}
else if (#q.qutyp_refID <= 2)
{
<td>#Html.TextBox("txtDateQuestions", DateTime.Today.ToString("dd/MM/yyyy"), new { style = "width: 120px" }) </td>
}
else
{
<td>#Html.TextBox(q.questionID.ToString(), null)</td>
}
</tr>
}
Thanks in Advance.........
Try like this,
#Html.CheckBoxFor(i => m.questionID, new { id = #m.questionID, #checked = "checked", Name = "CheckBox" })<span>m.description</span>
Example
Model
public class AssignProject
{
public Guid Id { get; set; }
public string EmployeesName { get; set; }
public Guid? EmployeeId { get; set; }
public Guid? ProjectId { get; set; }
public string AssignEmployeeId { get; set; }
public bool IsChecked { get; set; }
}
View
#foreach (var item in Model)
{
#Html.CheckBoxFor(m => item.IsChecked, new { value = item.EmployeeId, id = "chk_" + #item.EmployeeId, #checked = "checked", Name = "CheckBox" })
}