Pass An int value from view to Action from a link click - c#

I have a for loop in my view about a table. I am able to display the results. but when i click any of the links . i get a null point exception. i found that it is because that the loop is finished
#model List<MyMainModel>
#using System.Collections;
#using System.Web;
#{
ViewData["Title"] = "Display";
}
<h1>Display</h1>
<table border="1" cellpadding="20" style="table-layout:fixed">
<tr>
<th>
UserName
</th>
<th>
PAssword
</th>
<th>
Email
</th>
<th colspan="2">
Options
</th>
</tr>
#{ for (int i = 0; i < Model.Count; i++)
{
<tr>
<td>
#Model.ElementAt(i).USERNAME
</td>
<td>
#Model.ElementAt(i).PASSWORD
</td>
<td>
#Model.ElementAt(i).EMAIL
</td>
<td >
Remove
</td>
<td>
Edit
</td>
</tr>
}
}
</table>
When i click Remove i want to pass ID of the corresponding Element back to RemoveRow Action how do i do that

#Url.Action("RemoveRow", "MyMain", new { id = i })

Related

How to use EditorFor to save changes to the Model?

I have a view like this:
#model IEnumerable<Namespace.MyClass>
#using Newtonsoft.Json;
<form asp-action="Update">
<table class="table">
<thead>
<tr>
<th>
#Html.DisplayNameFor(model => model.Product)
</th>
<th>
#Html.DisplayNameFor(model => model.IsAvailable)
</th>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Product)
</td>
<td>
#Html.EditorFor(modelItem => item.IsAvailable)
</td>
</tr>
}
</tbody>
</table>
<div>
<a href=#Url.Action("SaveChanges", "Product", new {productList = JsonConvert.SerializeObject(Model) })> Save changes</a>
</div>
</form>
where SaveChanges looks like this:
[HttpGet] // Perhaps it should be HttpPost here, but if I change it to that nothing happens when I run the program after clicking the "Save changes" link
public ActionResult SaveChanges(string productList)
{
List<MyClass> products = JsonConvert.DeserializeObject<List<MyClass>>(productList);
// Here I continue with SqlCommand to later make an UPDATE to a database changing the value of IsAvailable which is the only adjustable value in my view
}
My issue is that when I run the program and I change IsAvailable (which is a bool) from false to true in my view and press "Save Changes", the model does not change, i.e. productList still has the value False for IsAvailable for that particular product.
Does anybody have an idea as to why? From my understanding if I have a and
Model information is not updated when you send the model to the action with the link. And the same initial amount is sent.
The changes you make to the model fields are only in the html controls and have nothing to do with the model. The model is updated when you post the form information.
I submitted the form to jQuery in the following code
in view
#model List<Namespace.MyClass>
#using (Html.BeginForm("SaveChanges", "Product", FormMethod.Post))
{
<table class="table">
<thead>
<tr>
<th>
#Html.DisplayNameFor(model => model.FirstOrDefault().Product)
</th>
<th>
#Html.DisplayNameFor(model => model.FirstOrDefault().IsAvailable)
</th>
</thead>
<tbody>
#for (int i = 0; i < Model.Count(); i++)
{
<tr>
<td>
#Html.DisplayFor(modelItem => Model[i].Product)
</td>
<td>
#Html.EditorFor(modelItem => Model[i].IsAvailable)
</td>
</tr>
}
</tbody>
</table>
<div>
#Html.ActionLink("Save changes", "", null, new { #id = "submit"})
</div>
}
#section scripts{
<script type="text/javascript">
$("#submit").click(function () {
document.forms[0].submit();
return false;
});
</script>
}
action in controller
[HttpPost] // Perhaps it should be HttpPost here, but if I change it to that nothing happens when I run the program after clicking the "Save changes" link
public ActionResult SaveChanges(IEnumerable<MyClass> products)
{
// Here I continue with SqlCommand to later make an UPDATE to a database changing the value of IsAvailable which is the only adjustable value in my view
}

How can I filter data in a Razor view with a dropdown

Hello I'm new to MVC and I'm trying to do a filtering for my index page, I want to be able to filter the graduation Status.
So when I go into the index I will see every status and I want to filter for example only the person with the graduated Status or the Pass/failed Status
Thank you in advance!
Index Page
Controller:
var graduates = db.Graduated_Students;
return View(graduates.ToList());
View:
<div class="content-body">
<div class="row">
<form action="#" method="post">
<div class="col-xs-12 col-sm-9 col-md-12">
<table class="table">
<tr>
<th>
#Resource.FirstName
</th>
<th>
#Resource.LastName
</th>
<th>
#Resource.CohortNumber
</th>
<th>
Placements
</th>
<th>
#Resource.GraduationStatus
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem =>
item.FirstName)
</td>
<td>
#Html.DisplayFor(modelItem =>
item.LastName)
</td>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.DisplayFor(modelItem =>
item.PartnerName)
</td>
<td>
#Html.DisplayFor(modelItem =>
item.GraduationStatus)
</td>
You could use linq in the View to filter the data
In the View
#foreach (var item in Model.Where(t=> t.GraduationStatus == <the graduation status selected in the dropdown> );
This approach Gets all the data from the database and then performs the filtering
That approach would work however a better approach would be to send the Selected GraduationStatus to the controller as a parameter and then perform the filtering directly on the db variable
In the Controller
public IActionResult GeStudents(GraduationStatus selectedGraduationStatus)
{
var graduates = db.Graduated_Students.Where(t=> t.GraduationStatus == selectedGraduationStatus);
return View(graduates.ToList());
}
This way you don't get all of the students from the database but only the ones which match the filter

Adding numbering for information from database

I am using ASP>NET MVC and pulling information from the database and displaying it on a view page. It is working fine but is there any way I can add Index numbers at the start?
For example, at present the display is as follows:
Name Age Gender
Annie 24 Female
Aaron 17 Male
I am looking to make it:
Index Name Age Gender
1 Annie 24 Female
2 Aaron 17 Male
I could have pulled the IDs from the database but they are not necessarily going to be in proper incremental values. I tried and increment via a ViewBag but it doesn't work. Was expected considering the increment was occurring outside the list.. My code as follows at the controller and view.
//Controller
public ActionResult Index()
{
int count = 1;
ViewBag.Count = count++;
return View(db.Person.ToList());
}
//View
<table class="table">
<tr>
<th>
Index
</th>
<th>
#Html.DisplayNameFor(model => model.Name)
</th>
<th>
#Html.DisplayNameFor(model => model.Age)
</th>
<th>
#Html.DisplayNameFor(model => model.Gender)
</th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#ViewBag.Count
</td>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.Age)
</td>
<td>
#Html.DisplayFor(modelItem => item.Gender)
</td>
</tr>
}
</table>
Declare the counter in the view (no need for the ViewBag property) and increment it inside the loop
#{ int counter = 1; }
<table class="table">
<tr>
<th>Index</th>
<th>#Html.DisplayNameFor(model => model.Name)</th>
....
</tr>
#foreach (var item in Model) {
<tr>
<td>#counter</td>
<td>#Html.DisplayFor(modelItem => item.Name)</td>
....
</tr>
counter++;
}
</table>
Use "for loop" in razor, something like:
#for(var i = 10; i < 21; i++)
{<p>Line #i</p>}

Passing list of data from controller to view

I'm trying to retrieve data in a specific order (to establish a ranking) with the below code:
public ActionResult ShowRanking(int id = 0)
{
Tournament tournament = db.Tournament.Find(id);
var parti = (from p in db.Participe
where p.IdTournament == tournament.IdTournament
//orderby p.ArchTotalScore descending
select p).OrderByDescending(x => x.ArchTotalScore);
//objlist = parti;
foreach (var part in parti)
{
tournament.Participe.Add(part);
//objlist.Add(part);
}
tournament.Participe.OrderByDescending(x => x.ArchTotalScore);
return View(tournament.Participe);
}
The data is retrieved but whenever I pass the list ofdata to my view, the order criterias I used are ignored and the records are shown as they have been inserted in the DB.
Here is my view as well:
#model ArcheryComp.Tournament
#{
ViewBag.Title = "Classement";
}
<h2>Classement</h2>
<table>
<tr>
<th>
Nom
</th>
<th>
Prenom
</th>
<th>
Division
</th>
<th>
Categorie
</th>
<th>
Score 1
</th>
<th>
Score 2
</th>
<th>
Total Score
</th>
</tr>
#foreach (var item in Model.Participe)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Archers.Nom)
</td>
<td>
#Html.DisplayFor(modelItem => item.Archers.Prenom)
</td>
<td>
#Html.DisplayFor(modelItem => item.Divisions.DivDescription)
</td>
<td>
#Html.DisplayFor(modelItem => item.Categorie)
</td>
<td>
#Html.DisplayFor(modelItem => item.ArchScore1)
</td>
<td>
#Html.DisplayFor(modelItem => item.ArchScore2)
</td>
<td>
#Html.DisplayFor(modelItem => item.ArchTotalScore)
</td>
<br />
</tr>
var tmp = item.IdTournament;
}
</table>
Do you have any idea what is wrong ? and could I correct this ?
Thanks in advance for your help.
You ordered the tournament.Participe, but you did not use the result. You must change it to something like this(if tournament.Participe is a Listofcourse):
tournament.Participe =
tournament.Participe.OrderByDescending(x => x.ArchTotalScore).ToList();
return View(tournament);
The model used in the view is ArcheryComp.Tournament therefore you must return View(tournament) not View(tournament.Participe).

Center Checkbox on its label in a Table in MVC

In my Project I am working on a few index views that only have a few fields. When their are few fields in a table the label and the checkbox below them never line up. As one example, in the code below the Active field is a checkbox and it is not under its label. How can I get it so the checkbox is centered on its label?
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.BranchName)
</th>
<th>
#Html.DisplayNameFor(model => model.Active)
</th>
<th></th>
</tr>
#foreach (var item in Model.OrderByDescending(m => m.Active).ThenBy(m => m.BranchName))
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.BranchName)
</td>
<td>
#Html.DisplayFor(modelItem => item.Active)
</td>
<td>
#Html.ActionLink("Edit", "EditBranch", new { id=item.BranchId })
</td>
</tr>
}
</table>
Here is a pic to see how ugly it looks:
Add css to the column, via colspan or direct on td or th
<td style="text-align: center">
OR
style="float:left"
Otherwise encapsulate the #Html.DisplayFor in a div with that style.
Finally don't have the css inline and move to the style.css file.

Categories