In my WebApp I consume a wcf web service and in my example I consume a Method that returns more than 400 value type. In the first the WebApp will show two column in the View(Table)
//Model
public class OrdersviewModel
{
public int Articlenr { get; set; }
public string ArticleName { get; set; }
}
Later the user has the possibility that he can add or show up new column
Value,The Question is how would be the model??
That is your model:
public class MyViewModel
{
public Dictionary<int, string> OrdersViewDictionary { get; set; }
}
into the view you could use dictionary like that
#model MyViewModel
<table class="table-bordered">
<thead>
<tr>
<th>Index key</th>
<th>Value</th>
</tr>
</thead>
<tbody>
#foreach (var pairData in Model.OrdersViewDictionary) {
<tr>
<td>#pairData.Key</th>
<td>#pairData.Value</th>
</tr>
}
</tbody>
</table>
Related
I want to allow the user to download the report, when it is approved by a supervisor. At the moment, I'm working with manager account, where he can check many reports and change their state to either verified or denied, but I don't understand why the report states enum list is not displaying, even though it is shown in the console.
HTML code
Model:
public class Report
{
public int ID { get; set; }
[Display(Name = "Report Name")]
public string reportName { get; set; }
public virtual User reportManager { get; set; }
[Display(Name = "State")]
public ReportState reportState { get; set; }
public byte[] reportData { get; set; }
}
public enum ReportState
{
Accepted,
Pending,
Denied
}
Controller:
public async Task<IActionResult> Index()
{
ViewBag.Reports = await _context.Reports.ToListAsync();
ViewBag.ReportStates = new SelectList(Enum.GetNames(typeof(ReportState)));
return View();
}
#model variable_pay_system.Models.Report
#{
ViewData["Title"] = "Reports";
}
<div class="container">
<h5>Reports</h5>
<table>
<thead>
<tr>
<th>
#Html.DisplayNameFor(model => model.reportName)
</th>
<th>
#Html.DisplayNameFor(model => model.reportState)
</th>
</tr>
</thead>
<tbody>
#foreach (Report report in ViewBag.Reports)
{
<tr>
<td>
#Html.DisplayFor(modelItem => report.reportName)
</td>
<td>
<select asp-for="#report.reportState" asp-items="Html.GetEnumSelectList<ReportState>()"></select>
</td>
</tr>
}
</tbody>
</table>
If you are going to use enums in View, you can show these enums as a list. and I recommend using it by printing it to a dropdown. EnumDropDownListFor()
Model :
public enum ReportState
{
Accepted,
Pending,
Denied
}
View:
#Html.EnumDropDownListFor(model => model.ReportState)
Using Tag Helper (ASP.NET MVC 6):
<select asp-for="#Model.SelectedValue" asp-items="Html.GetEnumSelectList<ReportState>()">
Ok so the problem was because, I was missing this code at the end.
<script>
$(document).ready(function () {
$('select').formSelect();
});
I want to create a Dashboard of two tables and a chart of data in the database using ASP.net Core 3.1 and EF Core 3.
This the teacher model class:
public class Teacher
{
[Key]
public Int64 ID { get; set; }
public Sring Name { get; set; }
public int Age { get; set; }
}
this is the course model class :
public class Course
{
[Key]
public Int64 ID { get; set; }
public Sring Name { get; set; }
public Sring TargetClass { get; set; }
}
The ViewModel class is as follows:
public class DashboardViewModel
{
public List<Course> Course { get; set; }
public List<Teacher> Teacher { get; set; }
}
the index page is :
<h2> Table of Teacher</h2>
<table>
<thead>
<tr>
<th> ID</th>
<th> Name</th>
<th> Age</th>
</tr>
</thead>
<tbody>
#foreach (Teacher item in Model.Teacher)
{
<tr>
<td>
#item.ID
</td>
<td>
#item.Name
</td>
<td>
#item.Age
</td>
</tr>
}
</tbody>
</table>
<h2> Table of Courses</h2>
<table>
<thead>
<tr>
<th> ID</th>
<th> Name</th>
<th> TargetClass</th>
</tr>
</thead>
<tbody>
#foreach (Teacher item in Model.Course)
{
<tr>
<td>
#item.ID
</td>
<td>
#item.Name
</td>
<td>
#item.TargetClass
</td>
</tr>
}
</tbody>
</table>
The home controller is :
public class HomeController : Controller
{
private readonly ApplicationDbContext _context;
public HomeController(ApplicationDbContext context)
{
_context = context;
}
public async Task<IActionResult> Index()
{
var qry = await _context.Teacher.ToListAsync();
var Teachers = new List<Teacher>(){};
var Courses = new List<Courses>() { };
var Dash = new DashboardViewModel {
Course = Courses,
Teacher = Teachers
};
return View(Dash);
}
The Problem appears when trying to pass the data from qry variable to list.
Question : How I can pass the data from EF to List of Teachers as the example?
var qry = _context.Teacher.ToListAsync();
var Teachers = new List<Teacher>(){qry};
Without convert error :
cannot convert from 'System.Threading.Tasks.Task<System.Collections.Generic.List<School.Models.Teacher>>' to 'School.Models.Teacher'
I can't do this in comments, so I will put this here and then I can amend the answer if it's not quite right. You don't need to create empty lists beforehand, just create the ViewModel all in one shot. As I think jegtugado was trying to suggest, the Index() code should look something like:
public async Task<IActionResult> Index()
{
var Dash = new DashboardViewModel {
Course = await _context.Course.ToListAsync(),
Teacher = await _context.Teacher.ToListAsync()
};
return View(Dash);
}
This is assuming your DbSets are called Teacher and Course (but these are often plural, so double check that).
I'm using paging with ASP.NET MVC, but I'm losing model data when navigating to next page.
Here is the code:
Partial view:
#using PagedList;
#using PagedList.Mvc;
#model Models.MyObject
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />
<table class="table table-striped table-hover sortable">
<thead>
<tr>
<th width="240">#Model.HeaderNames[0]</th>
<th width="240">#Model.HeaderNames[1]</th>
<th width="240">#Model.HeaderNames[2]</th>
<th width="240">#Model.HeaderNames[3]</th>
<th width="120" class="no-sort"></th>
</tr>
</thead>
<tbody>
#foreach (var member in Model.PagedModelList)
{
<tr>
<td><span class="sort-field-value">#member.Thead1</span></td>
<td><span class="sort-field-value">#member.Thead2</span></td>
<td><span class="sort-field-value">#member.Thead3</span></td>
<td><span class="sort-field-value">#member.Thead4</span></td>
</tr>
}
</tbody>
</table>
<footer>
<div>
Page #(Model.PagedModelList.PageCount < Model.PagedModelList.PageNumber
? 0 : Model.PagedModelList.PageNumber) of #Model.PagedModelList.PageCount
#Html.PagedListPager(Model.PagedModelList, page =>Url.Action("Reports",new { #page = page, FirstLoad = false }))
</div>
</footer>
Controller :
public ActionResult Reports(MyObject model, int? page, bool FirstLoad = true)
{
model.pageSize = 4;
model.pageNumber = (page ?? 1);
if (FirstLoad)
{
// getting the data from database here
// ... code
// assigning pagemodelist
model.PagedModelList = model.ModelList.ToPagedList(model.pageNumber, model.pageSize);
}
// here sending the model and all is good
return PartialView("_MyView", model);
}
Model
public class MyObject
{
public int SelectedPeriod { get; set; }
public List<SecondObject> ModelList = new List<Secondobject>();
public IPagedList<Secondobject> PagedModelList ;
public int pageSize { get; set; }
public int pageNumber { get; set; }
}
Second model class:
public class SecondObject
{
public string Thead1 { get; set; }
public string Thead2 { get; set; }
public string Thead3 { get; set; }
public string Thead4 { get; set; }
}
Expected to get next page but all I get is empty model which causes null reference when sending again to view from controller. What am I doing wrong here?
I'm getting right data in model the first time, I show the table with correct data, then when clicking on next page I debug in the controller so I got empty model.PagedModelList, even some other empty model properties .
Any help appreciated
Maybe, try to pass your model in parameters like :
#Html.PagedListPager(Model.PagedModelList, page =>Url.Action("Reports",new {#model = Model, #page = page, FirstLoad = false }))
Edit :
Another solution is to remove MyObject from parameters.
Load each time your data from your database. You can follow the example in
GitHub project
I am filling an SQL table with a query and then trying to pass the data from the model (for the table) into the view. I am attempting to use a ViewModel to pass the data to the view in a Controller Action.
Here is the Model:
public partial class Report
{
public int Id { get; set; }
public string Number { get; set; }
public Nullable<decimal> Amount { get; set; }
public Nullable<System.DateTime> Date { get; set; }
public Nullable<int> ReasonId { get; set; }
public string Notes { get; set; }
}
IEnumerable ViewModel:
public class ReportViewModel
{
public IEnumerable<Report> Reports { get; set; }
}
Controller Action:
[HttpPost]
public ActionResult UploadValidationTable(HttpPostedFileBase csvFile)
{
//Unrelated Code to read a CSV into another database table goes here
//But was removed so it wouldn't be confusing.
var db = new Report();
var reportModel = new ReportViewModel()
{
Reports = new List<Report>() {new Report()}
};
return View("Upload", reportModel);
}
View:
#model Entities.Models.ReportViewModel
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Number</th>
<th>Amount</th>
<th>Date</th>
<th>Reason Id</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
#if (Model != null)
{
foreach (var item in Model.Reports.Where(x => x.Id != null))
{
<tr>
<td>
#item.Id
</td>
<td>
#item.Number
</td>
<td>
#item.Amount
</td>
<td>
#item.Date
</td>
<td>
#item.ReasonId
</td>
<td>
#item.Notes
</td>
</tr>
}
}
</tbody>
</table>
But I get an exception when I try to return the View which says ReportViewModel is not assignable to model type IEnumerable ReportViewModel
So I am just trying to pass all the rows from the Report database to an HTML table in my view. Any help, or a better way to do this would be appreciated.
I have the following Model:
public class ContractPlain
{
public int Id { get; set; }
public Guid ContractGuid { get; set; }
public int SenderId { get; set; }
public int RecvId { get; set; }
public int ContractType { get; set; }
public string ContractStatus { get; set; }
public DateTime CreatedTime { get; set; }
public DateTime CreditEnd { get; set; }
}
public class Contrtacts
{
List<ContractPlain> listOutput;
public void Build(List<ContractPlain> listInput)
{
listOutput = new List<ContractPlain>();
}
public List<ContractPlain> GetContracts()
{
return listOutput;
}
internal void Build(List<contract> currentContracts)
{
throw new NotImplementedException();
}
}
As you can see, I defined a whole collection.
Why?
I need to render the data in table for user, because there are several rows which belongs to the exact/unique user (e.g. 20-30 shop items are referred to the single client).
So, I'm getting data from the DB using ADO.NET Entity. The binding question to the model instance in Controller is done and I don't have issues with that, I do with the rendering question only.
I think, it could be used with the #for stuff, but didn't know, how it would be better using especially my custom model.
So, how can I render the data in View using my model?
Thanks!
See the view below. You simply foreach over your collection and display the Contracts.
Controller:
public class ContactsController : Controller
{
public ActionResult Index()
{
var model = // your model
return View(model);
}
}
View:
<table class="grid">
<tr>
<th>Foo</th>
</tr>
<% foreach (var item in Model) { %>
<tr>
<td class="left"><%: item.Foo %></td>
</tr>
<% } %>
</table>
Razor:
#model IEnumerable<ContractPlain>
<table class="grid">
<tr>
<th>Foo</th>
</tr>
#foreach (var item in Model) {
<tr>
<td class="left"><#item.Foo></td>
</tr>
#}
</table>
If your action returns a List of contracts you can do the following in the view:
#model IEnumerable<ContractPlain>
#foreach(ContractPlain contract in Model)
{
<ul>
<li>#contract.ContractGuid</li>
<li>#contract.SenderId</li>
<li>#contract.ContractStatus</li>
<li>#contract.CreditEnd</li>
</ul>
}