Show Results on View - c#

Im triyng to make some calculation on my View, but i cant show the result out of my #{} block
I have this table.
<!--Table to display registered products-->
<table class="table table-condensed table-hover">
<thead>
<tr>
<th>CODIGO</th>
<th>NOME</th>
<th>PRECO</th>
<th>QUANTIDADE</th>
<th></th>
</tr>
</thead>
<tbody>
#if (Model != null)
{
double sum = 0;
for (int i = 0; i < Model.Item2.Count; i++)
{
<tr>
#using (Html.BeginForm("AddProduct", "Sale", FormMethod.Post))
{
<td>
#Html.DisplayFor(modelItem => Model.Item2[i].idProduct)
</td>
<td>#Html.DisplayFor(model => Model.Item2[i].nameProduct)</td>
<td>
<p>R$#Html.DisplayFor(model => Model.Item2[i].pricesaleProduct)</p>
</td>
<td>#Html.DisplayFor(model => Model.Item2[i].quantityProduct)</td>
<td>
<input type="submit" class="btn btn-danger" value="Deletar">
</td>
sum += Model.Item2[i].pricesaleProduct * Model.Item2[i].quantityProduct;
}
</tr>
}
}
</tbody>
</table>
<!--I want display "sum" here-->
Total Value: #sum
I want to show "sum" out of my table, but trying several variations and a get error!
How can i make it?

You have declared sum inside if(){} block and hence you're not able to access it. Move it outside like this and it'll work
double sum = 0;
#if (Model != null)
{
............
............
}

Related

Searching - from index view and result to another view

i'm creating a search function in mvc5
my program works like this:
Index view have search box and button
and the result of that also displays in index view
so my problem is that how do i display the result in another view - say like searchresult.cshtml and not in index view?
here's my controller:
public ActionResult Index(string searching)
{
return View(db.TblId.Where(x => x.IdNumber.Contains(searching) || searching == null));
}
my index view (i just removed the other text contents, only included the search result)
#model IEnumerable<MVC5_Search.Models.TblId>
#using (Html.BeginForm("Index", "Home", FormMethod.Get))
{
#Html.TextBox("searching")<input type="submit" value="Search" />
}
<table>
<thead>
<tr>
<td>Id Number</td>
<td>First Name</td>
<td>Middle Name</td>
<td>Last Name</td>
</tr>
</thead>
<tbody>
#if (Model.Count() == 0)
{
<tr>
<td colspan="3" style="color: red">
No Result!
</td>
</tr>
}
else
{
foreach (var item in Model)
{
<tr>
<td>#item.IdNumber</td>
<td>#item.Firstname</td>
<td>#item.Middlename</td>
<td>#item.Lastname</td>
</tr>
}
}
</tbody>
</table>
i'm using this together with entity framework
EDIT:(trying to solve)
here's what i've done so far,
i created another controller (SearchingController) to avoid conflict with the main controller,
[HttpGet]
public ViewResult SearchResult(string searching)
{
return View("SearchResult", db.TblId.Where(x => x.TId.Contains(searching) || searching == null));
}
and then the view SearchResult.cshtml
#model IEnumerable<TblId.Models.TId>
#{
ViewBag.Title = "searchresult";
}
<table>
<thead>
<tr>
<td>IdNumber</td>
<td>First Name</td>
<td>Middle Name</td>
<td>Last Name</td>
</tr>
</thead>
<tbody>
#if (Model.Count() == 0)
{
<tr>
<td colspan="3" style="color: red">
No Result!
</td>
</tr>
}
else
{
foreach (var item in Model)
{
<tr>
<td>#item.IdNumber</td>
<td>#item.Firstname</td>
<td>#item.Middlename</td>
<td>#item.Lastname</td>
</tr>
}
}
</tbody>
</table>
and in my index view,
#using (Html.BeginForm("SearchResult", "Searching", FormMethod.Get))
{
#*#Html.TextBox("searching")*#
<input type="text" id="searching" name="searching" />
<button type="submit" name="searching" id="searching "class="btn btn-secondary">
Verify
<br>
</button>
}
still not working as expected
when i clicked search button, it just change the url to something like this
/Index?searching=T101&searching=
T101 - is the Id i'm searching
Follow these steps:
-> Create a new view searchresult.cshtml with below contents
#model IEnumerable<MVC5_Search.Models.TblId>
<table>
<thead>
<tr>
<td>Id Number</td>
<td>First Name</td>
<td>Middle Name</td>
<td>Last Name</td>
</tr>
</thead>
<tbody>
#if (Model.Count() == 0)
{
<tr>
<td colspan="3" style="color: red">
No Result!
</td>
</tr>
}
else
{
foreach (var item in Model)
{
<tr>
<td>#item.IdNumber</td>
<td>#item.Firstname</td>
<td>#item.Middlename</td>
<td>#item.Lastname</td>
</tr>
}
}
</tbody>
</table>
-> Modify your index post action to
[HttpPost]
public ViewResult Index(string searching)
{
return View("searchresult",db.TblId.Where(x => x.IdNumber.Contains(searching) || searching == null));
}

How to Set Checkbox Value to True by Default using Html Helper [duplicate]

This question already has answers here:
Make a checkbox checked or unchecked based on the value?
(8 answers)
Closed 4 years ago.
I'm using the below given table in Partial view and I want to set the value of checkboxes to true by default using Html Helper. I have tried
<input type="checkbox" value="#Model[i].IsSelected" checked="checked" />
but it passes the false value to controller even if I checked it to true. Here is my table code. .
#model List<SchoolManagment.Models.tbl_Student>
<table id="tableforposting" class="table table-hover table-bordered">
<thead>
<tr>
<th>Sr#</th>
<th class="col-sm-1"></th>
<th>STUDENT NAME</th>
<th>ROLL NO.</th>
</tr>
</thead>
<tbody>
#for (int i = 0; i < Model.Count(); i++)
{
<tr>
<td class="col-sm-1 text-center">
#Html.CheckBoxFor(x => x[i].IsSelected)
</td>
<td>
#Html.DisplayFor(x => x[i].Name)
</td>
<td>
#Html.DisplayFor(x => x[i].Roll_No)
#Html.HiddenFor(x => x[i].Roll_No)
</td>
</tr>
}
</tbody>
</table>
In Controller action, you can set the Checkbox model property true to create the checked checkbox.
public ActionResult Index(Register model)
{
model.Terms = true;
return View(model);
}
In View,
#Html.CheckBoxFor(m => m.Terms)
For reference, How to set a CheckBox by default Checked in ASp.Net MVC

Best method for having single editor fields in an Enumerable form?

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).

Can I modify multiple items in a BeginForm section in a Razor/MVC view [duplicate]

This question already has answers here:
Multiple checkboxes in razor (using foreach)
(3 answers)
Closed 7 years ago.
I would like to edit a list of items and save them all together in the same submit. Is it possible? If so, how?
I have the following piece of code, but it does not give the wanted result. Or otherwise I don't know what to write for the counterpart in the controller.
#using (Html.BeginForm("Save", "MyController", FormMethod.Post))
{
<fieldset>
<table class="table table-striped table-hover ">
<thead>
<tr>
<th>Name</th>
<th>Value</th>
<th>Datum</th>
<th>NewValue</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#Html.HiddenFor(modelItem => item.Id)
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.Value)
</td>
<td>
#Html.TextBoxFor(modelItem => item.Value)
</td>
</tr>
}
</tbody>
</table>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
Update Thanks to #Jonesopolis I came up with this working solution
#using (Html.BeginForm("Save", "MyController", FormMethod.Post))
{
<fieldset>
<table class="table table-striped table-hover ">
<thead>
<tr>
<th>Name</th>
<th>Value</th>
<th>Datum</th>
<th>NewValue</th>
</tr>
</thead>
<tbody>
#for (int i = 0; i < Model.Count(); i++)
{
<tr>
<td>
#Html.HiddenFor(modelItem => modelItem[i].Id)
#Html.DisplayFor(modelItem => modelItem[i].Name)
</td>
<td>
#Html.DisplayFor(modelItem => modelItem[i].Value)
</td>
<td>
#Html.TextBoxFor(modelItem => modelItem[i].Value)
</td>
</tr>
}
</tbody>
</table>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
For MyController
public class MyController : Controller
{
[HttpPost]
public ActionResult Save(IEnumerable<NameDoesNotMatter> newValues)
{
...
}
}
public class NameDoesNotMatter
{
public int Id { get; set; }
public decimal? Value { get; set; }
}
Now see if i can work out the thing with the templates. The link of #StephenMuecke should be sufficient
Update 2
Well that's wasn't that hard the code is now
#using (Html.BeginForm("Save", "MyController", FormMethod.Post))
{
<fieldset>
<table class="table table-striped table-hover ">
<thead>
<tr>
<th>Name</th>
<th>Value</th>
<th>Datum</th>
<th>NewValue</th>
</tr>
</thead>
<tbody>
#Html.EditorForModel()
</tbody>
</table>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
And for Views/Shared/EditorTemplates/TypeOfModel.cshtml
#model TypeOfModel
<tr>
<td>
#Html.HiddenFor(item => item.Id)
#Html.DisplayFor(item => item.Name)
</td>
<td>
#Html.DisplayFor(item => item.Value)
</td>
<td>
#Html.TextBoxFor(item => item.Value)
</td>
</tr>
The controller remains the same
When you just show items using a for loop then the naming convention used in the resulting html tag doesn't match up with what MVC is expecting when it binds the model upon form submit. The easiest way to fix this is to use #Html.EditorFor and custom editor templates. Here are a couple of examples:
ASP.NET MVC DisplayTemplate and EditorTemplates for Entity Framework DbGeography Spatial Types
Extending Editor Templates for ASP.NET MVC

select all tr of a table and send it to the controller using checkbox , in MVC 4

I want to use the Check box for selecting multiple users and sending the result to my controller.
At first I was only sending the number of mobile users to the controller but it became necessary to send more than the number, I need number and name user, my controller and my view are as follows:
Would somehow send the number and name for example?
#using (Html.BeginForm("Enviar", "Home")) {
<table id="myTable">
<thead>
<tr>
<th>
<input type="checkbox" />
</th>
<th>#Html.DisplayName("Nome")</th>
<th>#Html.DisplayName("CANCELADO")</th>
<th>#Html.DisplayName("Numero")</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model) {
<tr>
<td>
<input type="checkbox" name="CELULAR" value="#item.CELULAR" />
</td>
<td name="Nome">#Html.DisplayFor(modelItem => item.NOME)</td>
<td name="Email">#Html.DisplayFor(modelItem => item.CANCELADO)</td>
<td name="Celular" value="#item.CELULAR">#Html.DisplayFor(modelItem => item.CELULAR)</td>
</tr>
}
</tbody>
</table>
<input type="submit" value="Selecionar"/>
}
The controller that receives:
[HttpPost]
public ActionResult Enviar(String[] celular) {
.....
return View();
}
When posting collections, you must index them correctly. Therfore, you must use a for loop instead of a foreach.
Also, why does your HttpPost take a String[] and not your Model? First change that to be your model type.
Assuming your model is: #model List<YourType>
Change your HttpPost to take that:
[HttpPost]
public ActionResult Enviar(List<YourType> model) {
.....
return View();
}
Now we'll rewrite your foreach into a for and use the CheckBoxFor helper. Also, add HiddenFor fields for any properties you want to see on post:
#for (int i = 0; i < Model.Count; i++)
{
<tr>
<td>
#Html.CheckBoxFor(m => m[i].CELULAR)
</td>
<td name="Nome">
#Html.HiddenFor(m => m[i].NOME)
#Html.DisplayFor(m => m[i].NOME)
</td>
<td name="Email">
#Html.HiddenFor(m => m[i].CANCELADO)
#Html.DisplayFor(m => m[i].CANCELADO)
</td>
<td name="Celular" value="#m[i].CELULAR">
#Html.DisplayFor(m => m[i].CELULAR)
</td>
</tr>
}

Categories