Search view : MVC - asp.net-mvc

I need to make a "Search and filter view"
My problem:
I want to create a Search view where the user selects by which attribute he wants to search and than enters something in textbox and submits it. The view than goes to controller finds what it needs to find and displays it
My view goes into the part that is written for when the HttpPost function in controller is done. But it goes in early. I have trouble displaying my results here's controller code part:
public ActionResult Filter()
{
return View();
}
[HttpPost]
public ActionResult Filter(string option, string search, FormCollection fc)
{
var bll = new STUDIJSKIPROGRAMBLLProvider();
StudijskiProgramViewModel model = new StudijskiProgramViewModel();
if (option == "skracenicaSmjer")
{
// For now I just putted some random number to get some data displayed
var program = bll.Fetch(2004);
model.StudijskiProgram = program;
return View(model);
}
else if (option == "skracenicaProfil")
{
return View();
}
else
{
return View();
}
}
And View Code:
#model SolutionC.Models.StudijskiProgramViewModel
#{
ViewBag.Title = "Pretraga studijskih programa";
}
#using (Html.BeginForm("Filter", "StudijskiProgram", FormMethod.Get))
{
<h2>Pretraga studijskih programa</h2>
<b> Kriteriji pretrage: </b>
<input type="radio" name="prvi" value="prvi1"/> <text>Skraćenica profil</text>
#Html.RadioButton("option", "skracenicaSmjer") <text>Skraćenica smjer</text>
#Html.RadioButton("option", "skracenicaProfil") <text> Skraćenica profil </text>
#Html.RadioButton("option", "ECTSOpterecenje")<text>ECTS</text>
#Html.TextBox("search")
<input type = "submit" name = "submit" value = "Traži" />
}
#using (Html.BeginForm("Filter", "StudijskiProgram", FormMethod.Post))
//Don't want my view to display this until i click Submit and my controller sets up data for display
{
<table class="table table-striped">
<thead class="alert alert-info">
<tr>
<th>
Naziv smjera:
</th>
<th>
Naziv profila:
</th>
<th>
Šifra:
</th>
</tr>
</thead>
<tr>
<td> #Model.StudijskiProgram.smjer </td> //Compilation fails here
<td> #Model.StudijskiProgram.profil</td>
<td> #Model.StudijskiProgram.IDStudProg </td>
</tr>
</table>
}

You got it all wrong.
#using (Html.BeginForm("Filter", "StudijskiProgram", FormMethod.Post))
Only means "Make me a form tag wrapper with the following attributes` not only show this when button is posted.
You should instead wrap it with a condition, that will only be met when the model property is set.
if (Model.SomeProperty)
{
<table class="table table-striped">
...
}
The bool SomePropery should be declared on your Model and be set to true when you want the post section to appear.
Note: You should return View(model) when wanting the View to know your model.

Related

Incorrent model passed to Partial View

This might be a duplicate question but I can't for the life of me know what the solution is. I'm trying to load a partial view with a table based on the ID of a clicked link.
I'm getting this error:
System.InvalidOperationException: 'The model item passed into the ViewDataDictionary is of type 'System.Collections.Generic.List`1[MasigasigTrackingSystem.Models.Invoice]', but this ViewDataDictionary instance requires a model item of type 'MasigasigTrackingSystem.Pages.Masigasig.MainPage.TripTicket.TripTicketListModel'.'
I can't seem to figure out how should I passed the model to the partial view.
Here is what my OnGet looks like:
public IList<Invoice> Invoice { get; set; }
public PartialViewResult OnGetDetails(int TripTicketID)
{
var invoices = from m in _context.Invoice
select m;
invoices = invoices.Where(x => x.TripTicketID == TripTicketID);
Invoice = invoices.ToList();
return Partial("_TripTicketListDetails", Invoice);
}
Here is my partial view:
#model TripTicketListModel
<h1>Hello, test</h1>
<table id="TripEntryList" class="table table-bordered table-hover">
<thead>
<tr>
<th>
#Html.DisplayNameFor(model => model.Invoice[0].InvoiceNumber)
</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model.Invoice)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.InvoiceNumber)
</td>
</tr>
}
</tbody>
You Partial View Required TripTicketListModel and you are passing the model Invoice. It means you are passing wrong model.
From your View code i can say your Invoice is used inside TripTicketListModel, so try below solution.
public PartialViewResult OnGetDetails(int TripTicketID)
{
var invoices = from m in _context.Invoice select m;
invoices = invoices.Where(x => x.TripTicketID == TripTicketID);
TripTicketListModel model = new TripTicketListModel();
model.Invoice = invoices.ToList();
return Partial("_TripTicketListDetails", model);
}

How do I select an Id from a SelectList using asp.net core 3?

I'm iterating through a list and populating a table with the last column having a edit button to edit that Id specific request.
Right now no matter what button i click it always takes me to the edit page of the first Id in the list, also the url has each Id listed in it like this.
/EditRequest?SelectedId=127&SelectedId=128
why is the SelectedId set to all values in the list? and how do I only pass the one Id of the one selected?
Here's my model
public class MyRequestsViewModel
{
public MyRequestsViewModel()
{
this.MyRequests = new List<SelectListItem>();
}
public List<SelectListItem> MyRequests;
public int SelectedId { get; set; }
}
I'm iterating through MyRequests and want to send SelectedId to the controller
<form method="get" asp-controller="Home" asp-action="EditRequest">
<table id="SortRequestsTable" class="table table-striped">
<thead>
<tr>
<th>SortID</th>
<th>SortCriteria</th>
<th>Edit</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model.MyRequests)
{
<tr>
<td>#item.Value</td>
<td>#item.Text</td>
<td>
<input asp-for="SelectedId" type="hidden" value="#item.Value" />
<button>#item.Value<span class="sap-icon"></span></button>
</td>
</tr>
}
</tbody>
</table>
</form>
And my controller keeps saying that SelectedId is 0
public IActionResult EditRequest(MyRequestsViewModel requests)
I got it working with this. But I don't want to display the Id value in the button.
<input asp-for="SelectedId" type="submit" value="#item.Value" /><span class="sap-icon icon-16"></span>
I've also tried using asp-route-SelectedId tag helper but I'm not entirely sure how to implement that.
Try code below to achieve Edit from Index Page.
<a asp-action="Edit" asp-route-id="#item.ID">Edit</a> |
And Controller for accept request
// GET: Tickets/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var ticket = await _context.Tickets.FindAsync(id);
if (ticket == null)
{
return NotFound();
}
return View(ticket);
}

Partial View is not rendering td elements inside foreach razor tag

I am using a List of Products as a Model for my Partial view. In the partial view I am using a foreach loop to get the values of Product class and trying to render it to users. In the partial view I've written this code:
#model Project.ViewModel.ListofProductsVM
#using Project.Models
<table>
<tr>
<th>
Name
</th>
<th>
Cost
</th>
</tr>
#{
foreach (Products prod in Model.products)
{
<tr>
<td>#prod.Name.ToString()</td>
<td>#prod.Cost.ToString()</td>
</tr>
}
}
</table>
I can see the headers "Name" and "Cost" in the page but Product Name and its Cost is not rendering. When I am checking the source code in HTML it is showing me this HTML only:
<table>
<tr>
<th>
Name
</th>
<th>
Cost
</th>
</tr>
</table>
While debugging I can see the values are correctly getting passed to Partial Views. Even the "#prod.Name" and "#prod.Cost" is showing me the correct value. But it is not rendering it to HTML. I don't understand what I am doing wrong here. Thanks in advance.
The #{ } means that this is c# code block, you can fix the problem by writing your foreach loop code this way:
#foreach (Products prod in Model.products)
{
<tr>
<td>#prod.Name.ToString()</td>
<td>#prod.Cost.ToString()</td>
</tr>
}
and if you want to stick with your way then you have to specify to render that as html like:
#{
foreach (Products prod in Model.products)
{
#:<tr>
#:<td>#prod.Name.ToString()</td>
#:<td>#prod.Cost.ToString()</td>
#:</tr>
}
}
I had a similiar problem. In my case, all I needed was to add "#" before the foreach nested in an if statement. This is a pared down snippet of my working code
C# Model class:
public class MyModel
{
public string FirstName{ get; set; }
public string LastName{ get; set; }
}
CSHTML file:
#model List<MyProject.Models.MyModel>
#if (Model.Count > 0)
{
<div class="row" >
<table>
#foreach (var item in Model)
{
<tr>#item.FirstName #item.LastName</tr>
}
</table>
</div>
}
But it's the same answer that another user gave you.

How to display a collection in View of ASP.NET MVC Razor project?

How to display a collection in View of ASP.NET MVC Razor project?
My Model and View is below. For one person i've to display Many tests on the screen. Thanks in Advance
namespace UI.Models
{
public class SuperStudent
{
public string FullName { get; set; }
public ICollection<TestDetail> CandidateTestDetails { get; set; }
}
public class TestDetail
{
public DateTime TestDate { get; set; }
public string RegNum { get; set; }
}
}
View
#model IEnumerable<UI.Models.SuperStudent>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.FullName)
</th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
Print all test details
</td>
</tr>
}
</table>
For one person your view must be like :
#model UI.Models.SuperStudent
<h2>#Model.FullName</h2>
<table class="table">
<tr>
<th>
TestDate
</th>
<th>
RegNum
</th>
</tr>
#foreach (var item in Model.CandidateTestDetails ) {
<tr>
<td>
#item.TestDate
</td>
<td>
#item.RegNum
</td>
</tr>
}
</table>
The code works the same inside a loop as it does outside the loop. Just reference the loop item. For example:
#foreach (var student in Model) {
#foreach (var test in student.CandidateTestDetails) {
<tr>
<td>#test.RegNum</td>
</tr>
}
}
That would output just the RegNum value. You can also make use of things like DisplayNameFor by referencing an element in the collection:
#foreach (var student in Model) {
#foreach (var test in student.CandidateTestDetails) {
<tr>
<td>
#Html.DisplayNameFor(model => student.CandidateTestDetails.First().RegNum)<br />
#test.RegNum
</td>
</tr>
}
}
While intuitively it may seem that this could fail if Model is empty (since First() would throw an exception), it doesn't actually execute First() on the collection. The engine simply uses that to reflect into the model returned by First() to determine the display name.
Given this, you can construct the markup however you wish for those items. If it's complex enough, you might even create a partial view for just an item in that collection and render that partial view in the loop by passing the item value to the partial view.

bind a table on click event of button

I am working with ASP.NET MVC 3 Razor. I have a list method in controller, so want to bind this list to table on click event of a button. How will I achieve this functionality.
Here is my controller method:
public ActionResult getItnList(string scanCode)
{
List<List<String>> getitnDetails_List = getitnDetails(date, name);
ViewBag.getitnDetails = getitnDetails_List;
return Json(new { getitnDetails_List = getitnDetails_List }, JsonRequestBehavior. AllowGet);
}
Here is my view code:
#{
List<List<String>> str = (List<List<String>>)ViewBag.getitnDetails;
}
<table id="list" width="100%">
<td><b>Harvest Date</b></td>
<td><b>Product</b></td>
#for (int i = 0; i <= str.Count - 1; i++)
<td>#str[i][0].ToString()</td>
<td>#str[i][1].ToString()</td>
</table>
How will I bind this table to list on click event of button?
I'm not exactly sure what you are asking. But if you want to get the list on the click of a button you can use jQuery .getJSON (http://api.jquery.com/jQuery.getJSON/) to retrieve the list data and then insert it with javascript. Or you could render the list in the action and simply use json .get and insert the result.
Try this
View
<table>
<tr>
<th>
Name
</th>
<th>
PhoneNo
</th>
<th>
Address
</th>
<th>
Action
</th>
</tr>
#{var list = ViewBag.RegisterItems; }
#if (list != null)
{
foreach (var m in list)
{
<tr>
<td>
<input id="txtName-#m.ID.ToString()" type="text" class="hide" value="#m.Name.ToString()"/>
</td>
<td>
<input id="txtPhoneNo-#m.ID.ToString()" type="text" class="hide" value="#m.PhoneNo.ToString()"/>
</td>
<td>
<input id="txtAddress-#m.ID.ToString()" type="text" class="hide" value="#m.Address.ToString()"/>
</td>
</tr>
}
}
</table>
Controller
public ActionResult CustomerInfo()
{
ViewBag.RegisterItems = GetAllRegisterData();
return View();
}

Resources