I am creating an MVC application. When you use scaffolding templates with your Controllers the Index page is constructed using a table and a foreach which creates a row and the appropriate cells for each instance of the Model in the collection you provide like so:
#model IEnumerable<PoliticiOnline.DTO.Question>
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
#Html.DisplayNameFor(model => model.GeneralQuestion)
</th>
<th>
#Html.DisplayNameFor(model => model.Explanation)
</th>
<th>
#Html.DisplayNameFor(model => model.IsTemplate)
</th>
<th>
#Html.DisplayNameFor(model => model.DateSubmitted)
</th>
<th>
#Html.DisplayNameFor(model => model.JudgementDate)
</th>
<th>
#Html.DisplayNameFor(model => model.FbShares)
</th>
<th>
#Html.DisplayNameFor(model => model.FbLikes)
</th>
<th>
#Html.DisplayNameFor(model => model.TwitterShares)
</th>
<th>
#Html.DisplayNameFor(model => model.SiteVotes)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.GeneralQuestion)
</td>
<td>
#Html.DisplayFor(modelItem => item.Explanation)
</td>
<td>
#Html.DisplayFor(modelItem => item.IsTemplate)
</td>
<td>
#Html.DisplayFor(modelItem => item.DateSubmitted)
</td>
<td>
#Html.DisplayFor(modelItem => item.JudgementDate)
</td>
<td>
#Html.DisplayFor(modelItem => item.FbShares)
</td>
<td>
#Html.DisplayFor(modelItem => item.FbLikes)
</td>
<td>
#Html.DisplayFor(modelItem => item.TwitterShares)
</td>
<td>
#Html.DisplayFor(modelItem => item.SiteVotes)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id=item.QuestionId }) |
#Html.ActionLink("Details", "Details", new { id=item.QuestionId }) |
#Html.ActionLink("Delete", "Delete", new { id=item.QuestionId })
</td>
</tr>
}
</table>
I would like to change this view to something like a listview which contains like a quick look-card for each instance. How would I go about creating this using html and CSS? Basically I would like to create my own structure and present the user with a list with a brief view of each model instance (they can of course see the detail on the detail page).
I hope my explanation was clear, it's very difficult to explain what I mean. You could compare it to defining custom controls, like creating an alternate design for each item in a listview. Hope someone can help me!
EDIT
I actually mean something like the homepage of Stackoverflow. I want to achieve the same sort of structure. I'd like a list with short information of each question. I took a look at the source of Stackoverflow and the CSS but can't really find how to do it. How could you define number of votes goes in the left, title in the middle and author info in bottom right?
I think this is a pretty broad html question and you will probably need to look at some html and css tutorial to really get you going. Your code COULD be as simple as this
#model IEnumerable<PoliticiOnline.DTO.Question>
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
#foreach (var item in Model) {
<div style="border: 1px solid black;">
#Html.DisplayFor(modelItem => item.GeneralQuestion ) <br />
#Html.DisplayFor(modelItem => item.Explanation )
</div>
}
Which will produce a bunch of ugly boxes with some text in it. You will really need to understand css to make it look nicer than that. I recommend reading the "Learn Html" and "Learn CSS" sections of http://www.w3schools.com/ .
Related
I summed up the values in my view and I wanted to send the total back to my controller but I'm not sure if there is a way to do that.
Index.cshtml
#model IEnumerable<Rest.Models.ShoppingCartItem>
#{
ViewData["Title"] = "Index";
}
<h1>Shopping Cart</h1>
<table class="table">
<thead>
<tr>
<th>
#Html.DisplayNameFor(model => model.item)
</th>
<th>
#Html.DisplayNameFor(model => model.price)
</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Menuitem.ItemName)
</td>
<td>
#Html.DisplayFor(modelItem => item.Menuitem.Price)
</td>
<td>
<a asp-action="Delete" asp-route-id="#item.ID">Delete</a>
</td>
</tr>
}
</tbody>
</table>
<h1>
#Model.Sum(i => i.Menuitem.Price)
</h1>
Is there a way I can send the value I computed back to my controller?
Also, this is my first time using HTML - how do I make the font and formatting look nice? Can someone point me towards some good references?
Thanks!
I'm trying to display a table from a model. But the problem is that my model class has a string that already includes an <a> tag.
My view is:
#model MyWeb.Models.Incidente
#{
Layout = null;
ViewBag.Title = "Communication for " + Model.num.ToString();
}
<h2>#ViewBag.Title</h2>
<table class="table">
<tr>
<th> User </th>
<th> Date </th>
<th> Time </th>
<th> Message </th>
</tr>
#foreach (MyWeb.Models.Comunication item in Model.GetComunications())
{
<tr>
<td> #Html.DisplayFor(modelItem => item.user) </td>
<td> #Html.DisplayFor(modelItem => item.date) </td>
<td> #Html.DisplayFor(modelItem => item.time) </td>
<td> #Html.DisplayFor(modelItem => item.message) </td>
</tr>
}
</table>
The problem is that one item in GetComunications() has a message that include a link to Google:
You can Google it, just press Here!. Thank you!
So when the table is displayed the message shows the tag instead of creating the link in "Here!".
How can I solve this problem, and show a message that already has a link on the string?
If you are sure about the sanitation of your output, you can use Html.Raw:
#Html.Raw(Html.DisplayFor(modelItem => item.message))
i added PagedList in my project, i have a search view, which basically calls a function to get values from database, my Pagedlist is working fine, but i am not sure if this gets whole data from database or only based on pageSize. For example i have total 30 records, and i added pagesize as 10, so view will show 10 records at a time and 3 pages. Now this PagedList get all 30 values, or only get 10 first time, then if user click on page 2 it gets next 10 records.
Because lets say i have thousand records, so i dont want system to load all of them, i just want to load it according to pagesize , and when user click on second page it gets next records, hope my point is clear, below is my code:
View:
#model PagedList.IPagedList<BackendIntegration.Models.VendorAllTransactions>
#using PagedList.Mvc;
#if (Model != null)
{
<hr />
<div class="form-horizontal">
<table class="table">
<tr class="bg-primary">
<th>
#Html.DisplayName("Date")
</th>
<th>
#Html.DisplayName("Type")
</th>
<th>
#Html.DisplayName("Amount")
</th>
<th>
#Html.DisplayName("User Name")
</th>
<th>
#Html.DisplayName("Vendor Name")
</th>
<th>
#Html.DisplayName("POS ID")
</th>
</tr>
#foreach (var trans in Model)
{
<tr>
<td>
#Html.DisplayFor(model => trans.Date)
</td>
<td>
#Html.DisplayFor(model => trans.TypeDesc)
</td>
<td>
#Html.DisplayFor(model => trans.Amount)
</td>
<td>
#Html.DisplayFor(model => trans.UserName)
</td>
<td>
#Html.DisplayFor(model => trans.VendorName)
</td>
<td>
#Html.DisplayFor(model => trans.POS_ID)
</td>
</tr>
}
</table>
<br />
<div id='Paging' style="text-align: center">
Page #(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber)
of #Model.PageCount
#Html.PagedListPager(Model, page => Url.Action("VendorAllTransactions", new { page }))
</div>
</div>
}
Controller:
public ActionResult VendorAllTransactions(VendorAllTransactionView vw, int? page)
{
int pageSize = 10;
int pageIndex = 1;
pageIndex = page.HasValue ? Convert.ToInt32(page) : 1;
IPagedList<VendorAllTransactions> model = null;
try
{
model = sales.VendorAllTransactions(vw).ToPagedList(pageIndex, pageSize);
}
catch (Exception exp)
{
ModelState.AddModelError("", exp.Message);
}
return View(model);
}
sales.VendorAllTransactions(vw) this call a fucntion with query, which will return records. But this clearly returning all the records. Is Pagedlist get only required number of rows(pagesize) or it gets everything.???
Please advise.
Thanks
I have a week entity and a day entity which are tables and controllers for each. As it stands in my application, you have to create the week then the days separately. In the weeks tab, if you click on "Details" for a week, you can see the days and the data in them in a table.
My question is, how do you create a button that would bring you to the DayController create page so that I can make the days from the week tab.
Here is the HTML for the Week Details, I am new to this and I figure this is the code that would help you understand:
#model Utilities.Models.Week
#{
ViewBag.Title = "Details";
}
<h2>Details</h2>
<div>
<h4>Week</h4>
<hr />
<dl class="dl-horizontal">
<dt>
#Html.DisplayNameFor(model => model.WeekID)
</dt>
<dd>
#Html.DisplayFor(model => model.WeekID)
</dd>
<dt>
#Html.DisplayNameFor(model => model.WeekNo)
</dt>
<dd>
#Html.DisplayFor(model => model.WeekNo)
</dd>
<dt>
#Html.DisplayNameFor(model => model.Days)
</dt>
<dd>
<table class="table">
<tr>
<th>
WeekID
</th>
<th>
DayNo
</th>
<th>
Day of the Week
</th>
<th>
Data 1
</th>
<th>
More data
</th>
<th></th>
</tr>
#foreach (var item in Model.Days)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Week.WeekID)
</td>
<td>
#Html.DisplayFor(modelItem => item.DayNo)
</td>
<td>
#Html.DisplayFor(modelItem => item.DayofWeek)
</td>
<td>
#Html.DisplayFor(modelItem => item.Data1)
</td>
<td>
#Html.DisplayFor(modelItem => item.More Data)
</td>
</tr>
}
</table>
</dd>
</dl>
</div>
<p>
/*
| My attempt at going to the create day page below but it doesn't work. Am I referencing it wrong?
| Basically, I want to click "Create Day" and go to the create page for Day.
*/
#Html.ActionLink("Create Day", "Create", new { id = Model.Days.DayID}) |
#Html.ActionLink("Edit", "Edit", new { id = Model.WeekID }) |
#Html.ActionLink("Back to List", "Index")
</p>
I can include any other code that might be needed to help answer.
Days is the navigation property in the Week model.
When I run the code above, I get:
CS1061: 'System.Collections.Generic.ICollection<Utilities.Models.Day>' does not contain a definition for 'DayID' and no extension method 'DayID' accepting a first argument of type 'System.Collections.Generic.ICollection<Utilities.Models.Day>' could be found (are you missing a using directive or an assembly reference?)
but Day does have a DayID.
With that error I suppose your Model.Days is a collection of Day objects, so that can find a property named DayID. To do what you wand you need to specify the index of the object you want in that collection. Try to do Model.Days[0].DayID for example and tell me if that works, please.
Code:
#model Utilities.Models.Week
#{
ViewBag.Title = "Details";
}
<h2>Details</h2>
<div>
<h4>Week</h4>
<hr />
<dl class="dl-horizontal">
<dt>
#Html.DisplayNameFor(model => model.WeekID)
</dt>
<dd>
#Html.DisplayFor(model => model.WeekID)
</dd>
<dt>
#Html.DisplayNameFor(model => model.WeekNo)
</dt>
<dd>
#Html.DisplayFor(model => model.WeekNo)
</dd>
<dt>
#{
var count = 0;
foreach(var day in Model.Days)
{
#Html.DisplayNameFor(model => day.DayofWeek)
if(count!= Model.Days - 1)
{ #(", ") }
count++;
}
</dt>
<dd>
<table class="table">
<tr>
<th>
WeekID
</th>
<th>
DayNo
</th>
<th>
Day of the Week
</th>
<th>
Data 1
</th>
<th>
More data
</th>
<th></th>
</tr>
#foreach (var item in Model.Days)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Week.WeekID)
</td>
<td>
#Html.DisplayFor(modelItem => item.DayNo)
</td>
<td>
#Html.DisplayFor(modelItem => item.DayofWeek)
</td>
<td>
#Html.DisplayFor(modelItem => item.Data1)
</td>
<td>
#Html.DisplayFor(modelItem => item.MoreData)
</td>
<td>
#Html.ActionLink("Create Day", "Create", new { id = item.DayID})
</td>
</tr>
}
</table>
</dd>
</dl>
</div>
<p>
#Html.ActionLink("Edit", "Edit", new { id = Model.WeekID }) |
#Html.ActionLink("Back to List", "Index")
</p>
UPDATE:
If you want to create a new day just change you code with this:
<p>
#Html.ActionLink("Create Day", "Create", "Day")
#Html.ActionLink("Edit", "Edit", new { id = Model.WeekID }) |
#Html.ActionLink("Back to List", "Index")
</p>
i want to delete last row.
i have dive within div , which has table and many rows.
i wanted to delete last row of table. i am unable to iterate to last tr.
<div class="span15" id="MURWorksheetPage2">
<div class="span8">
<table>
<tr>
<td class="col1">
#Html.DisplayNameFor(model => model.MedicineUseNo) #Html.TextBoxFor(model => model.MedicineUseNo, new { id = "txtMedicineUseNo", style = "Width:20px" })
</td>
<td class="col2">#Html.DisplayNameFor(model => model.MedicinePrescribed) #Html.TextBoxFor(model => model.MedicinePrescribed, new { id = "txtMedicinePrescribed" })</td>
</tr>
<tr>
<td class="col1" colspan="2">
#Html.DisplayNameFor(model => model.OTC) #Html.TextBoxFor(model => model.OTC, new { id = "txtOTC" })
</td>
</tr>
</table>
</div>
</div>
Not tested, but this should work.
$('#MURWorksheetPage2 div table :last').remove();