I'm working on an application in which I retrieve data from a database to be displayed in a table in my cshtml. One of the fields I'm calling is a date field. The data for it in the database is stored in the following format: MMDDYY i.e; 091504. For reasons I need not get into, the data has to be stored in that format in the database. I'm trying to figure out how I can modify its display to show 09/15/04 instead of 091504.
#model IEnumerable<BillingApp.Models.SERV_CHARGE_INT_RATE_NSF_PENALTY>
<table id="Test" class="tablesorter">
<thead>
<tr>
<th>Effective Date</th>
</tr>
</thead>
#if (Model.Count() == 0)
{
<tbody>
<tr>
<td colspan="4">
No Records match search criteria
</td>
</tr>
</tbody>
}
else{
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.EFFECTIVE_DATE)
</td>
</tr>
}
</tbody>
</table>
If you use a DateTime object, you can try using [DisplayFormat] in your model.
Ex :
[DisplayFormat(DataFormatString = "{MM/dd/yy}")]
public DateTime EFFECTIVE_DATE { get; set }
If you are using a string instead of DateTime, you could consider converting the data type by using #ByteBlast's solution :
DateTime.ParseExact(item.EFFECTIVE_DATE, "MMddyy", CultureInfo.CurrentCulture);
There are many ways to handle this.
One would be to have your model class parse the dates coming out of the database into actual DateTime objects and then use those throughout your application (probably the best choice, if you need to do anything else with the dates).
Another would be to slice up the string when displaying it, if all you need to do is display the date. Something along the lines of this:
#foreach (var item in Model)
{
<tr>
<td>
#(item.EFFECTIVE_DATE.Substring(0,2))/#(item.EFFECTIVE_DATE.Substring(2,2))/#(item.EFFECTIVE_DATE.Substring(4,2))
</td>
</tr>
}
Related
What I wanted to do was seeming simple but I am having a bit of trouble. I've created the following class:
// SelectedItem.cs
public class SelectedItem
{
public Brand Brand { get; set; }
public string Model { get; set; }
}
In my view page I've created a list of SelectedItem:
// Chart.cshtml
#model Prediction.Models.Chart.ManualSelection
#{
#using Prediction.Models.Chart
List<SelectedItem> selectedItems = new List<SelectedItem>();
}
And I'm trying to add to this list from the html in my view. I've created a table that has its rows populated by a foreach loop.
// Also Chart.cshtml, below the previous code section where I created the list
<table class="table table-borderless table-hover table-striped table-sm mb-0">
<tbody>
#foreach (var item in Model.PhoneInfo)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Brand)
</td>
<td>
#Html.DisplayFor(modelItem => item.Model)
</td>
<td>
<button type="button" class="btn btn-sm btn-link">Select</button>
</td>
</tr>
}
</tbody>
</table>
Essentially what I'm trying to do is when the button is clicked, I want that specific item.Brand and item.Model to be added to List<SelectedItem> selectedItems.
Unfortunately this is impossible. Like it was mentioned in the comment, this list exists only in the scope of rendering view on the server, before sending to the client's browser. In order to save your item.Brand and item.Model you need to use JavaScript, that will be executed in browser on client side or you can send this information back to the server and process it there.
Small Question,
I am having the following in my c# mvc project code:
div class="card-body">
#if (User.IsInRole("Secretaris"))
{
<table class="table">
<tr>
<th>#Html.DisplayNameFor(model => model.FirstName)</th>
</tr>
#foreach (var item in Model)
{
if (User.IsInRole("Secretaris"))
{
<tr>
<td>#Html.DisplayFor(modelitem => item.FirstName)</td>
</tr>
}
}
</table>
}
</div>
When I run the code everything works fine but when I go into the foreach it still gives all the names and not only the names that have the role "Secretaris". Hope someone can help me in what I am doing wrong.
Thanks in advance.
Roel Knippen
Since your Model has a .Firstname property I assume your model represents a person, likely a user.
#User represents the currently logged in user - NOT the user from your model.
You will need to include role information in your model if you want to work with users/permissions in your model. Something like
if (modelItem.Roles.Contains("Sec...")) { }
To show the table to begin with, you check to see if the User has the Secretaris role. So it either will or will not show, depending on that role.
However, then within the foreach loop you check to see if the user is in that role again, which is redundant because that code won't even run if it's not due to your enclosing if statement.
I'm assuming you want to run some kind of check on the "item" object that is an element of the Model.
div class="card-body">
#if (User.IsInRole("Secretaris"))
{
<table class="table">
<tr>
<th>#Html.DisplayNameFor(model => model.FirstName)</th>
</tr>
#foreach (var item in Model)
{
if (item.role == "Secretaris")
{
<tr>
<td>#Html.DisplayFor(modelitem => item.FirstName)</td>
</tr>
}
}
</table>
}
</div>
Maybe something like this where you check if the item.role is secretaris?
I am making an mvc application-c# that uses data from a database and I am having trouble looping through the list in the view that is part of my model.
This is my code in the view:
#foreach (var item in Model.communications)
{
<tr>
int i = 0;
<td>#Model.student.FirstName #Model.student.LastName</td>
<td>#item.Notes</td>
<td>#item.Date</td>
<td>
#Model.employeeName[i]
</td>
</tr>
}
try to replace your code with this
use item variable instead of Model so you can access values
#foreach (var item in Model.communications)
{
<tr>
int i = 0;
<td>#item.student.FirstName #item.student.LastName</td>
<td>#item.Notes</td>
<td>#item.Date</td>
<td>
#item.employeeName[i]
</td>
</tr>
}
If someone loads say three or four entries, when they search and they select entry two to edit, when they click the open button, how would I make it go to say /entry/2 or if they opened entry three it would say /entry/3 ?
I'm new to ASP.NET MVC so I'm really not sure what I am doing:
#model IEnumerable<Savvy_CRM_MVC.Models.RootObject>
#{
Layout = null;
}
#foreach (var m in Model)
{
}
<table class="mui-table">
<thead>
<tr>
<th>Case ID</th>
<th>Forename</th>
<th>Surname</th>
<th>Postcode</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach (var m in Model)
{
<tr>
<td>#m.Caseid</td>
<td>#m.Forename</td>
<td>#m.Surname</td>
<td>#m.Postcode</td>
<td><button>Open Case</button></td>
</tr>
}
</tbody>
</table>
When clicking the button, I want the url to change and it to load the data of that entry to the page in a format that will be decided later.
Your best bet is to change the button into a hyperlink:
#foreach (var m in Model)
{
<tr>
<td>#m.Caseid</td>
<td>#m.Forename</td>
<td>#m.Surname</td>
<td>#m.Postcode</td>
<td><a href="/entry/"+#m.CaseId>Open Case</a></td>
</tr>
}
This will generate a GET request to the "entry/{id}" route.
Hope this helps
I am sending data through HttpClient (Json) from console app to web app, everything is working well but the only issue Iam facing that data sent from controller to view is not displayed. I debugged the program and i can see that data are returned to the view. Following are the codes:
Constroller:
using ( var db = new MessageDBContext())
{
try
{
db.Messages.Add(model);
db.SaveChanges();
}
catch (DbEntityValidationException e)
{
Debug.WriteLine("Saving Messages into db Error 1: " + e.EntityValidationErrors);
}
}
return View(model);
Controller Debugg:
and here is the view code:
<table class="table table-striped">
<thead>
<tr>
<th>Message</th>
<th>Time</th>
</tr>
</thead>
<tbody>
#if (Model != null)
{
<tr>
<td>
#Html.Display(#Model.message)
</td>
<td>
#Html.Display(#Model.time)
</td>
</tr>
}
</tbody>
</table>
View Debugg:
So why the data are not displayed to the html view!?
You have to use one of this case:
#if (Model != null)
{
<tr>
<td>
#Html.DisplayFor(model => model.message)
</td>
<td>
#* Or *#
#Model.time
</td>
</tr>
}
I'll add some information to this question. The controller method which (shown in the question) is marked with HttpPost attribute and is called from the console application. Of course, the result is returned to the console and NOT to the browser. In the browser the controller returns empty model return View(); Otherwise, the DisplayFor method would work perfectly. If you want to update the display in your browser you need to write more code to establish server->client communication.