I'm reading content from some API, and the content may come as either come as "paragraph" or "ordered list".
My list order is always restarting numbering every time it reads a new record.
Example
1.TEST
1.TEST
My goal is to display the content as follows
1.TEST
2.TEST
even after reading different content in between
#foreach (var x in item.copy)
{
if (x.type == "o-list-item")
{
<tr>
<td>
<ol>
<li>
#Html.DisplayFor(modelItem => x.text)
</li>
</ol>
</td>
</tr>
}
else if(x.type== "paragraph")
{
<tr>
<td>
#Html.DisplayFor(modelItem => x.text)
</td>
</tr>
}
}
I understand that if the list sequence is interrupted, then it is a new list sequence, and can’t be a continuation of a previous list.
But, I have to make it, and
I tried to custom code myself, by concatenating an integer count=1 and increment when the for loop reads "o-list-item" type of content, but the DisplayFor razor view seems not allowing to concatenate any custom made variables.
#foreach (var x in item.copy)
{
if (x.type == "o-list-item")
{
int count=1;
<tr>
<td>
<ol>
<li>
count + "."+ #Html.DisplayFor(modelItem => x.text)
count ++;
</li>
</ol>
</td>
</tr>
}
else if(x.type== "paragraph")
{
<tr>
<td>
#Html.DisplayFor(modelItem => x.text)
</td>
</tr>
}
}
Any help please
I don't know a lot of C# and haven't seen this #foreach annotated like this yet. But maybe you could have some method that receives the text, from this method build a string concatenating the counter and the text received. Something like:
public List<String> receivesFromApi() {
return new ArrayList<String>();
}
public String iteratesThroughList() {
int counter = 1;
for(String data : receivesFromApi()){
System.out.println(counter+"."+data);
}
}
You can use Ordered List attribute "start" to specify start number.
#{int count = 1;}
#foreach (var x in item.copy)
{
if (x.type == "o-list-item")
{
<tr>
<td>
<ol start="#count++">
<li>
#Html.DisplayFor(modelItem => x.text)
</li>
</ol>
</td>
</tr>
}
else if(x.type== "paragraph")
{
<tr>
<td>
#Html.DisplayFor(modelItem => x.text)
</td>
</tr>
}
}
Related
I have created a very simple search form in my MVC application which allows the user to search for a string term and returnt he results. I would like to let my users search between two dates but I'm not sure how I'd program that.
Here is my code so far. As you'll notice this is only for searching for a string that is passed into the controller as a parameter. I have started writing the code for the date pickers but I've gotten stuck.
View
<h2>Search</h2>
#using (Ajax.BeginForm("Search", "Search", new AjaxOptions
{
OnSuccess = "Success",
OnFailure = "Failure",
UpdateTargetId = "test",
InsertionMode = InsertionMode.Replace
}))
{
<table class="table">
<tr>
<td><label>Vessel Name</label></td>
<td>#Html.TextBox("term",null, new { #class = "k-textbox" })</td>
</tr>
<tr>
<td>
<label>From</label>
#(Html.Kendo().DatePicker()
.Name("date_from")
.Value(DateTime.Today)
)
</td>
<td>
<label>To</label>
#(Html.Kendo().DatePicker()
.Name("date_to")
.Value(DateTime.Today)
)
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Search" />
</td>
</tr>
</table>
}
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Location</th>
<th>MMSI</th>
<th>Created</th>
</tr>
</thead>
<tbody id="test">
#Html.Partial("_results")
</tbody>
</table>
Controller
public ActionResult Search(string term, DateTime date_from, DateTime date_to)
{
var vessels = (from o in db.vessels
select o);
if (!String.IsNullOrEmpty(term))
{
vessels = vessels.Where(s =>
s.vessel_name.Contains(term) ||
s.vessel_location.Contains(term) ||
s.vessel_mmsi.Contains(term));
}
return PartialView("_results", vessels);
}
Partial View
Since I'm using ajax to update a target ID I return the results as in a partial view using the 'replace' insertion method.
#model IEnumerable<Multiple_Table_Post.Models.vessel>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.vessel_name)
</td>
<td>
#Html.DisplayFor(modelItem => item.vessel_location)
</td>
<td>
#Html.DisplayFor(modelItem => item.vessel_mmsi)
</td>
<td>
#Html.DisplayFor(modelItem => item.created)
</td>
</tr>
}
As you'll notice I have started putting the date pickers in place but I don't know how to write the c# to allow me to search between them. I'm passing them into the method as datetime parameters but after is where I'm totally stuck.
Many thanks
You could do this in either of the LINQ queries you have shown.
var vessels= (from o in db.vessels
where (o.created>= date_from && o.created<= date_to)
select o);//I have not tested this.
More Info:
C# Linq Where Date Between 2 Dates
You can also filter the dates in the second LINQ query as seen in the other answers
var vessels = (from o in db.vessels
where o.vessel_date => date_from
&& o.vessel_date =< date_to
&& ( term==null
|| o.vessel_name.Contains(term)
&& o.vessel_location.Contains(term)
&& o.vessel_mmsi.Contains(term))
select o);
I have a form, that just runs through a model and returns all the rows,. pretty basic MVC one:
#model IEnumerable<Something.Else.Model>
<table>
<thead>
<tr>
<th>#Html.DisplayNameFor()</th>
<th>#Html.DisplayNameFor()</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>#Html.DisplayFor(modelItem => item.)</td>
<td>#Html.DisplayFor(modelItem => item.)</td>
</tr>
}
</tbody>
</table>
However, what I want is to load an editor row when a button is clicked, and the editor fields to reference the model because of the validation I have on the model.
#model IEnumerable<Something.Else.Model>
<table>
<thead>
<tr>
<th>#Html.DisplayNameFor()</th>
<th>#Html.DisplayNameFor()</th>
</tr>
</thead>
<tbody>
<tr style="display: none"> // editor row
<td>#Html.EditorFor()</td>
<td>#Html.TextBox()</td>
</tr>
#foreach (var item in Model)
{
<tr>
<td>#Html.DisplayFor(modelItem => item.)</td>
<td>#Html.DisplayFor(modelItem => item.)</td>
</tr>
}
</tbody>
</table>
The issue I am having is that the form is Enumerable, so when I try to load a single row i get the message IEnumerable does not contain a definition for "field name" and no extension method etc etc
I know I could use #Html.Editor instead of editor for, but I have a lot of validation on the model I want to use. I could put these rows in the #foreach section and load the top one and use jquery to clear the values but that seems like a bad way.
The purpose of this is to enter a new line, not edit an existing one.
What is the most efficient way of achieving this?
One way would be to replace foreach with for with indexing of items, add a placeholder (empty) item at the end of the collection and display the editing item on button click:
#model List<Something.Else.Model>
<table>
<thead>
<tr>
<th>#Html.DisplayNameFor(m => m.First)</th>
<th>#Html.DisplayNameFor(m => m.Second)</th>
</tr>
</thead>
<tbody>
<tr id="edit-item" style='display: none'>
<td>
#Html.EditorFor(m => Model[Model.Count - 1].First)
</td>
<td>
#Html.EditorFor(m => Model[Model.Count - 1].Second)
</td>
</tr>
#for (var i = 0; i < Model.Count - 1; i++) // the last item is the placeholder (editing item)
{
<tr>
<td>
#Html.DisplayFor(m => Mode[i].First)
#Html.HiddenFor(m => Mode[i].First)
</td>
<td>
#Html.DisplayFor(m => Mode[i].Second)
#Html.HiddenFor(m => Mode[i].Second)
</td>
</tr>
}
</tbody>
</table>
Model type is changed to List to allow indexing of items.
The controller would need to determine from the values in the placeholder if the placeholder is filled (an item was entered).
Ok so I basically have a survey wizard with 4 parts in it, I need to display 5 separate results into each, but a for loop as below, will obviously display all results and will therefore break the wizard, how can I loop through this in a for loop?
Any help will be greatly appreciated, thanks
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Category.Title)
</td>
<td>
#Html.DisplayFor(modelItem => item.Survey.Title)
</td>
<td>
#Html.DisplayFor(modelItem => item.Title)
</td>
</tr>
}
Assuming your Model is some sort of IENumerable
#{ var list = Model.ToList();
for (int i = 0; i < list.Count; i++)
{
<tr>
<td>
#Html.DisplayFor(modelItem => list[i].Category.Title)
</td>
<td>
#Html.DisplayFor(modelItem => list[i].Survey.Title)
</td>
<td>
#Html.DisplayFor(modelItem => list[i].Title)
</td>
</tr>
}
}
Go it, just used an incrementer, seems to work just fine with the Model
#{
int counter = 0;
foreach (var item in Model)
{
counter++;
if (counter > 1 && counter < 6)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Category.Title)
</td>
<td>
#Html.DisplayFor(modelItem => item.Survey.Title)
</td>
<td>
#Html.DisplayFor(modelItem => item.Title)
</td>
</tr>
}
}
}
I'm trying to display records in my view. I only want to display records that don't have a null value for the TABLE_NUMBER1 field. I've set this up, but am having a weird issue where it won't recognize the closing tag for the else, only the foreach and if. Specifically, if I add a closing tag for the else, it recognizes it as the closing tag for the entire razer section (aka it's gold).
#if (Model.Count() == 0)
{
<tbody>
<tr>
<td colspan="7">
No Records match search criteria
</td>
</tr>
</tbody>
}
else{
foreach (var item in Model)
{
var hiderows = #Html.DisplayFor(modelItem => item.TABLE_NUMBER1).ToString();
if (hiderows != null)
{
<tbody>
<tr>
<td>
<p class="one">#Html.DisplayFor(modelItem => item.PNTR_MAX)</p>
</td>
<td>
<p class="one">#Html.DisplayFor(modelItem => item.EFFECTIVE_DATE1)</p>
</td>
<td>
<p class="one">#Html.DisplayFor(modelItem => item.BANK_ROUTING_NUMBER)</p>
</td>
<td>
<p class="one">#Html.DisplayFor(modelItem => item.TAX_ID)</p>
</td>
<td>
<p class="one">#Html.DisplayFor(modelItem => item.BANK_NAME)</p>
</td>
<td>
<p class="one">#Html.DisplayFor(modelItem => item.COMPANY_NAME1)</p>
</td>
<td>
#Html.ActionLink("Edit", "..\\Table9\\Edit", new { id = item.EFTID })
#Html.ActionLink("Details", "..\\Table9\\Details", new { id = item.EFTID })
#Html.ActionLink("Delete", "..\\Table9\\Delete", new { id = item.EFTID })
</td>
</tr>
</tbody>
}
}
}
This is wrong:
var hiderows = #Html.DisplayFor(modelItem => item.TABLE_NUMBER1);
change it to :
#Html.DisplayFor(modelItem => item.TABLE_NUMBER1)
if (item.TABLE_NUMBER1!= null)
{
I have a following problem: I have form, which has some dropdowns, textareas etc. and also I have two tables:
- SelectedItems
- AvailableItems
This two tables are a result of rendering two lists in my Model. What I need to do is when user press add button in a defined item row then I move item from AvailableItems to SelectedItems. My idea was to operate on lists so add to one, remove from another. I have no idea how to update lists of objects which can be found in model
public class Model
{
public List<Item> SelectedItems {get; set;}
public List<Item> AvailableItems {get; set;}
//other properties
}
My Partial View is:
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Description</th>
<th></th>
</tr>
</thead>
#if (Model.SelectedItems != null)
{
foreach (var item in Model.SelectedItems)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.ID)
</td>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.Description)
</td>
<td>
#Html.ActionLink("Add", "RemoveItem", new { ItemID = item.ID}, new { #class = "btn btn-primary" })
</td>
</tr>
}
}
</table>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Description</th>
<th></th>
</tr>
</thead>
#if (Model.AvailableItems != null)
{
foreach (var item in Model.AvailableItems)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.ID)
</td>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.Description)
</td>
<td>
#Html.ActionLink("Add", "AddItem", new { ItemID = item.ID}, new { #class = "btn btn-primary" })
</td>
</tr>
}
}
</table>
Problem is that I can't reload whole page. I need to make inserts do db basing on that form so is there any possibility to update model and then start inserts?
Thank you for your help.