Search by multiple values - asp.net-mvc

How can i make my search engine accept inputs more than one which are seperated by comma?
This is my index.view:
<p>
#using (Html.BeginForm("Index", "Home", FormMethod.Get))
{
<b>Search by:</b> #Html.RadioButton("searchby", "ItemID", true)<text>Item Id</text>
<br />
#Html.TextBox("search") <input type="submit" value="Search" />
}
</p>
<table>
<tr>
<th>
ItemID
</th>
<th>
Item name
</th>
</tr>
#if (Model.Count() == 0)
{
<tr>
<td colspan="2">no item id found.</td>
</tr>
}
else
{
foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.itemid)
</td>
<td>
#Html.DisplayFor(modelItem => item.itemname)
</td>
</tr>
}
<caption>Total #Html.Encode(Model.Count()) item found.</caption>
}
</table>
And this is my homecontroller.cs:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using itemdb.Models;
namespace itemdb.Controllers
{
public class HomeController : Controller
{
private mydbEntities db = new mydbEntities();
public ActionResult Index(string searchBy, string search)
{
if (!String.IsNullOrEmpty(searchBy) && !String.IsNullOrEmpty(search))
{
if (searchBy == "itemid")
{
return View(db.itemlist.Where(x => x.items == search).ToList());
}
else
{
return View(db.itemlist.Take(0));
}
}
}
}

public ActionResult Index(string searchBy, string search)
{
if (!String.IsNullOrEmpty(searchBy) && !String.IsNullOrEmpty(search))
{
string[] items = search.Split(new Char[] {','});
if (searchBy == "itemid")
{
return View(db.itemlist.Where(x => items.Contains(x.items)).ToList());
}
else
{
return View(db.itemlist.Take(0));
}
}

Related

How to Bring Child's Name to List?

enter image description hereI intend to return the name of the child with the vaccines already administered.
what happens is that it does not return anything to me, since if I call the Id it returns the Id of the Child
Model VacinacaoIndexData
using GestCentros.Models;
using System.Collections.Generic;
namespace GestCentros.ViewModels
{
public class VacinacaoIndexData
{
public IEnumerable<Vacinacao> Vacinacoes { get; set; }
public IEnumerable<Vacina> Vacinas { get; set; }
public IEnumerable<Enrollment> Enrollments { get; set; }
}
}
Controler
public ActionResult Index(int? id, int? Idvacina)
{
var user = db.Users.Where(u => u.UserName == User.Identity.Name).FirstOrDefault();
if (user == null)
{
return RedirectToAction("Index", "Home");
}
var viewModel = new VacinacaoIndexData();
viewModel.Vacinacoes = db.Vacinacao
.Include(v => v.Vacinas)
.Include(v => v.Crianca)
.Where(c => c.IdCentro == user.IdCentro);
if (id != null)
{
ViewBag.IdVacinacao = id.Value;
viewModel.Vacinas = viewModel.Vacinacoes
.Single(i => i.IdVacinacao == id.Value).Vacinas;
}
if (Idvacina == null) return View(viewModel);
ViewBag.IdVacina = Idvacina.Value;
// Lazy loading
viewModel.Enrollments = viewModel.Vacinas.Single(x => x.IdVacina == Idvacina).Enrollments;
// Explicit loading
Vacina selectedVacina = viewModel.Vacinas.Single(x => x.IdVacina == Idvacina);
db.Entry(selectedVacina).Collection(x => x.Enrollments).Load();
foreach (var enrollment in selectedVacina.Enrollments)
{
db.Entry(enrollment).Reference(x => x.Centro).Load();
}
viewModel.Enrollments = selectedVacina.Enrollments;
return View(viewModel);
}
In the Field Name I would like to Show Child Name, but it does not return anything
#model GestCentros.ViewModels.VacinacaoIndexData
#{
ViewBag.Title = "Index";
}
<h2 class="text-center">Lista de Crianças Vacinadas</h2>
<table class="table">
<tr>
<th>Nome</th>
<th>Data Vacinação</th>
<th>Vacinas</th>
<th>Operações</th>
</tr>
#foreach (var item in Model.Vacinacoes)
{
string selectedRow = "";
if (item.IdVacinacao == ViewBag.IdVacinacao)
{
selectedRow = "success";
}
<tr class="#selectedRow">
<td>
#Html.DisplayFor(model => item.Crianca.Nome)
</td>
<td>
#item.DataVacina.ToString("dd/MM/yyyy")
</td>
<td>
#{
foreach (var vacina in item.Vacinas)
{
#: #vacina.Nome <br />
}
}
</td>
</tr>
}
</table>
#if (Model.Vacinas != null)
{
<h3>Vacinas Ministradas na Campanha</h3>
<table class="table">
<tr>
<th>Operações</th>
<th>Vacina</th>
</tr>
#foreach (var item in Model.Vacinas)
{
var selectedRow = "";
if (item.IdVacina == ViewBag.IdVacina)
{
selectedRow = "success";
}
<tr class="#selectedRow">
<td>
#Html.ActionLink("Selecionar", "Index", new { IdVacina = item.IdVacina }, new { #class = "btn btn-sm btn-secundary" })
| #Html.ActionLink("Voltar a Lista", "Index", new { }, new { #class = "btn btn-sm btn-default" })
</td>
<td>
#item.Nome
</td>
</tr>
}
</table>
}
#if (Model.Enrollments != null)
{
<h3>
Students Enrolled in Selected Course
</h3>
<table class="table">
<tr>
<th>Name</th>
<th>Grade</th>
</tr>
#foreach (var item in Model.Enrollments)
{
<tr>
<td>
#item.Centro.Name
</td>
<td>
#Html.DisplayFor(modelItem => item.Grade)
</td>
</tr>
}
</table>
}

How to save the settings of an authentication id in ASP.NET MVC

I have made an tv-guide where you can see the programs for 5 channels. If you login you can also make an "personal" tvguide. where you can decide which of the 5 channels you are interested in seeing the tableau of, by clicking on their checkboxes. How can I make the application remember the user input. So let's say I log in and open the menu for Channel one (SVT1), and two (SVT2). Then I want these two channels also to be open next time I log in with that user id, how do I do that?
Screenshot of my personal tv-guide page
My controller
public class FavoritChannelsController : Controller
{
TvProgramDBEntities db = new TvProgramDBEntities();
[Authorize(Roles = "User")]
public ActionResult channel_index()
{
List<Full> model = new List<Full>();
var list = db.Full.Where(d => d.Date == "2018-11-12").ToList();
foreach (var item in list)
{
if (item.Id <1000)
{
var initedF = new Full
{
Channel = Regex.Replace(item.Channel, #"\s", ""),
Program = item.Program,
Time = item.Time,
Date = Regex.Replace(item.Date, #"\s", "")
};
model.Add(initedF);
}
}
return View(model);
}
}
My view
#model IEnumerable<Uppgift4.Models.Full>
#{
ViewBag.Title = "channel_Index";
var list = Model.ToList();
var list1 = list.Where(_ => _.Channel == "SVT1").Select(_ => _.Time +
_.Program).ToList();
var list2 = list.Where(_ => _.Channel == "SVT2").Select(_ => _.Time +
_.Program).ToList();
var list3 = list.Where(_ => _.Channel == "TV3").Select(_ => _.Time +
_.Program).ToList();
var list4 = list.Where(_ => _.Channel == "TV4").Select(_ => _.Time +
_.Program).ToList();
var list5 = list.Where(_ => _.Channel == "Kanal5").Select(_ => _.Time +
_.Program).ToList();
}
(_ => _.Date + " " +_.Time + _.Program).ToList();
<style>
.hiddenRow {
padding: 0 !important;
}
.in-line {
display: inline;
}
</style>
<br />
<table class="table table-condensed" style="border-collapse:collapse;">
<thead>
<tr>
<th>
<h3>TV-Tablå 2018-11-12</h3>
<b>Välj de kanaler du vill se tablån för</b>
</th>
<th>
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="checkbox" data-toggle="collapse" data-target="div
[id*='demo1']" /><b> SVT1</b>
</td>
</tr>
#for (var i = 0; i < #list1.Count; i++)
{
<tr>
<td colspan="2" class="hiddenRow">
<div id="demo1+'#i'+" class="accordian-body collapse">
#list1[i]
</div>
</td>
</tr>
}
<tr data-toggle="collapse" data-target="#demo2">
<td>
<input type="checkbox" data-toggle="collapse" data-target="div
[id*='demo2']" /><b> SVT2</b>
</td>
</tr>
#for (var i = 0; i < #list2.Count; i++)
{
<tr>
<td colspan="2" class="hiddenRow">
<div id="demo2+'#i'+" class="accordian-body collapse">
#list2[i]
</div>
</td>
</tr>
}
<tr data-toggle="collapse" data-target="#demo3">
<td>
<input type="checkbox" data-toggle="collapse" data-target="div
[id*='demo3']" /><b> TV3</b>
</td>
</tr>
#for (var i = 0; i < #list3.Count; i++)
{
<tr>
<td colspan="2" class="hiddenRow">
<div id="demo3+'#i'+" class="accordian-body collapse">
#list3[i]
</div>
</td>
</tr>
}
<tr data-toggle="collapse" data-target="#demo4">
<td>
<input type="checkbox" data-toggle="collapse" data-target="div
[id*='demo4']" /><b> TV4</b>
</td>
</tr>
#for (var i = 0; i < #list4.Count; i++)
{
<tr>
<td colspan="2" class="hiddenRow">
<div id="demo4+'#i'+" class="accordian-body collapse">
#list4[i]
</div>
</td>
</tr>
}
<tr data-toggle="collapse" data-target="#demo5">
<td>
<input type="checkbox" data-toggle="collapse" data-target="div
[id*='demo5']" /><b> Kanal5</b>
</td>
</tr>
#for (var i = 0; i < #list5.Count; i++)
{
<tr>
<td colspan="2" class="hiddenRow">
<div id="demo5+'#i'+" class="accordian-body collapse">
#list5[i]
</div>
</td>
</tr>
}
</tbody>
</table>
My model (from my database table Full with entity framework)
namespace Uppgift4.Models
{
using System;
using System.Collections.Generic;
public partial class Full
{
public int Id { get; set; }
public string Channel { get; set; }
public string Program { get; set; }
public string Category { get; set; }
public string Date { get; set; }
public string Time { get; set; }
public string Length { get; set; }
}
}
By using following Command you can Get the UserId of the current user in controllers.
using Microsoft.AspNet.Identity;
.
.
.
User.Identity.GetUserId()
Use this Id to save/retrieve user choices. For example you can add following entity.
public class User_Channel
{
public int User_ChannelId { get; set; }
[MaxLength(128)]
public string UserId { get; set; }
public int ChannelId { get; set; }
}

how to get selected checkbox in asp.net using entity framework

i'm new to asp.net mvc.I have a list of checkboxes and i want when the checkboxes are selected a new list of selected checkboxs are shown.
my code Product.cs code:
public class Product
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public int Price { get; set; }
public bool Checked { get; set; }
public virtual ICollection<Purchase> Purchases { get; set; }
}
My view:
<h2>Product Lists</h2>
#using (Html.BeginForm())
{
<table class="table">
<tr>
<th>
Product ID
</th>
<th>
Product Name
</th>
<th>
Price
</th>
<th></th>
</tr>
#for (var i = 0; i < Model.Count(); i++)
{
<tr>
<td>
#Html.DisplayFor(x => x[i].ProductID)
</td>
<td>
#Html.DisplayFor(x => x[i].ProductName)
</td>
<td>
#Html.DisplayFor(x => x[i].Price)
</td>
<td>
#Html.CheckBoxFor(x => x[i].Checked, new { Style = "vertical-align:3px}" })
</td>
</tr>
}
</table>
<input type="submit" value="Purchase" class="btn btn-default" />
}
This is my Controller code.I want when the check boxes are selected in a new page the selected check boxes are shown.
my ActionResult:
public ActionResult Index()
{
return View(db.Products.ToList());
}
[HttpPost]
public ActionResult Index(List<Product> list)
{
return View(list);
}
#using (Html.BeginForm())
{
<table class="table">
<tr>
<th>
Product ID
</th>
<th>
Product Name
</th>
<th>
Price
</th>
<th></th>
</tr>
#for (var i = 0; i < Model.Count(); i++)
{
<tr>
<td>
#Html.DisplayFor(x => x[i].ProductID)
</td>
<td>
#Html.DisplayFor(x => x[i].ProductName)
</td>
<td>
#Html.DisplayFor(x => x[i].Price)
</td>
<td>
#Html.CheckBoxFor(x => x[i].Checked, new { Style = "vertical-align:3px}" })
</td>
</tr>
}
</table>
If you already have a List with all checked/unchecked properties and just want to show the checked records in a new view, you can store your list in a TempData and redirect to an action which will use your list:
public ActionResult Index()
{
return View(db.Products.ToList());
}
[HttpPost]
public ActionResult Index(List<Product> list)
{
TempData["CheckedRecords"] = list.Where(x=>x.Checked).ToList(); //Don't forget to add 'using System.Linq;'!
return RedirectToAction("MyOtherView");
}
public ActionResult MyOtherView()
{
var checkedRecords = (List<Product>)TempData["CheckedRecords"];
return View(checkedRecords);
}

Posted model is null

I'm not sure what's wrong since I'm very new with MVC. This is a shopping cart. The customer is able to review their cart and edit quantity.
On the HttpPost ViewCart method, the cart is always empty, and number of lines is zero.
Controller:
public ActionResult ViewCart() {
var cart = (CartViewModel)Session["Cart"];
return View(cart);
}
[HttpPost]
public ActionResult ViewCart(CartViewModel cart) {
Session["Cart"] = cart;
return RedirectToAction("Order", "Checkout");
}
View:
#model CartViewModel
using (Html.BeginForm()) {
<h2>Your cart</h2>
<table>
<thead> ... </thead>
<tbody>
#foreach (var item in Model.Lines) {
<tr>
<td>#Html.DisplayFor(modelItem => item.Article.Description)</td>
<td>#Html.EditorFor(modelItem => item.Quantity)</td>
</tr>
}
</tbody>
</table>
<input type="submit" value="Checkout">
}
ViewModel:
public class CartViewModel {
public List<Line> Lines { get; set; }
public CartViewModel() {
Lines = new List<Line>();
}
}
Try changing the view to use indexes:
#model CartViewModel
using (Html.BeginForm()) {
<h2>Your cart</h2>
<table>
<thead> ... </thead>
<tbody>
#for (int i = 0; i < Model.Lines.Count; i++) {
<tr>
<td>#Html.DisplayFor(m => Model.Lines[i].Article.Description) #Html.HiddenFor(m => Model.Lines[i].Article.Id)</td>
<td>#Html.EditorFor(m => Model.Lines[i].Quantity)</td>
</tr>
}
</tbody>
</table>
<input type="submit" value="Checkout">
}

Simple form submission gives missing resource on postback

Apologies if this seems mundane, but I am a huge beginner to MVC and this seemingly simple task is giving me a massive headache.
I can't understand why my code isn't working. I'm submitting a form from my Index.cshtml and the postback is telling me the resource /Index (the page I am posting from) doesn't exist.
Could I please get some help on what I am doing wrong?
My View (Index.cshtml):
#model MyProject.Connection
#{
ViewBag.Title = "Index";
}
<div id="container" class="container centered primary">
#using (Html.BeginForm(FormMethod.Post))
{
<img src="#Url.Content("/Content/images/default.png")" alt="CMGR Web" />
<div class="divider"></div>
<fieldset id="fs_server" class="borderless!T">
<legend>Server details</legend>
<table>
<tr>
<td>
#Html.LabelFor(c => c.serverHost)
</td>
<td>
#Html.TextBoxFor(c => c.serverHost)
</td>
</tr>
<tr>
<td>
#Html.LabelFor(c => c.instanceName)
</td>
<td>
#Html.TextBoxFor(c => c.instanceName)
</td>
</tr>
</table>
</fieldset>
<fieldset id="fs_user" class="borderless!T">
<legend>Your credentials</legend>
<table>
<tr>
<td>
#Html.LabelFor(c => c.username)
</td>
<td>
#Html.TextBoxFor(c => c.username)
</td>
</tr>
<tr>
<td>
#Html.LabelFor(c => c.password)
</td>
<td>
#Html.PasswordFor(c => c.password)
</td>
</tr>
</table>
</fieldset>
<div class="divider"></div>
<table>
<tr>
<td>
#Html.CheckBoxFor(c => c.remember)
#Html.Label("Remember me?")
</td>
<td>
<input type="submit"/>
</td>
</tr>
</table>
}
</div>
My Model (Connection.cs):
namespace MyProject.Models
{
public class Connection
{
[Display(Name="Server Host")]
public string serverHost { get; set; }
[Display(Name="Instance Name")]
public string instanceName { get; set; }
[Display(Name = "Username")]
public string username { get; set; }
[Display(Name = "Password")]
public string password { get; set; }
[Display(Name = "Remoting Password")]
public string remotingPassword { get; set; }
[Display(Name = "Persistent")]
public bool remember { get; set; }
}
}
My Controller (IndexController.cs)
namespace MyProject.Controllers
{
public class IndexController : Controller
{
//
// GET: /Login/
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
private ActionResult Index(Connection channel)
{
return View();
}
}
}
Your Post ActionResult is set to Private so it isnt accessible. Change it to public
private ActionResult Index(Connection channel)
{
return View();
}
public ActionResult Index(Connection channel)
{
return View();
}

Resources