Searching - from index view and result to another view - c#

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));
}

Related

Row to send a List<Object> from Razor page to Update?

I need to send a List back to my controller and update the values on my repository.
But after load the view with the values, update it and click the submit button, I don't know how to get the list with the update values and call the update method from the repository.
I'm using .Net 4.7.2.
HomeController:
[HttpGet]
public ActionResult Nota(string Concurso)
{
List<InscricoesModel> model = new List<InscricoesModel>();
InscricoesRepository repository = new InscricoesRepository();
model = repository.GetAprovadosPrimeiraFase(new Guid(Concurso));
return View("Nota",model);
}
[HttpPost]
public void UdateNotas(List<InscricoesModel> model)
{
InscricoesRepository repository = new InscricoesRepository();
foreach(InscricoesModel item in model)
{
repository.Update(item);
}
}
Nota.cshtml:
#model List<ConcursoBolsaSegundaFase.Model.InscricoesModel>
<h1>Classificados 2ª Fase</h1>
<hr />
<p>Exportado em #DateTime.Now</p>
<div style="margin-top:15px">
#* TABELA RESULTADO *#
<div id="notasAprovadosSegundaFase" style="margin-top:10px">
#using (Html.BeginForm("UdateNotas", "Home", Model, FormMethod.Post))
{
<table class="table table-bordered" id="tblNotaAprovadosSegundaFase">
<thead>
<tr>
<th>Inscrição</th>
<th>Nome</th>
<th>Nota Primeira Fase</th>
<th>Fez Carta</th>
<th>Nota Segunda Fase</th>
</tr>
</thead>
<tbody>
#if (Model != null)
{
foreach (var linha in Model)
{
<tr>
<td>#linha.Inscricao</td>
<td>#linha.Nome</td>
<td>#linha.NotaPrimeiraFase</td>
<td>
<select>
<option value="false">Não</option>
<option value="true">Sim</option>
</select>
</td>
<td><input type="text" value="#linha.NotaSegundaFase"></td>
</tr>
}
}
</tbody>
</table>
<button type="submit" class="btn btn-success">Salvar</button>
}
</div>
</div>
The UpdateNotas method in my controller never recive a value, i don't know how to send the list from the view to my controller.
In MVC, the name of the input will be binded to the variable in the controller. In your case, your input has no name. I suggest you look at the html helpers.
This would bind your values properly.
for (int i = 0;i<Model.Count;i++)
{
Html.TextBoxFor(model => Model[i].NotaSegundaFase)
}
In this case, only NotaSegundaFase would be sent back to the controller.
You can use #Html.HiddenFor() to hold the data on the view and bind to controller on Post.
#using (Html.BeginForm("UdateNotas", "Home", Model, FormMethod.Post))
{
<table class="table table-striped table-bordered table-sm table-responsive">
<thead>
<tr>
<th>Inscrição</th>
<th>Nome</th>
<th>Nota Primeira Fase</th>
<th>Fez Carta</th>
<th>Nota Segunda Fase</th>
</tr>
</thead>
<tbody>
#for (int i = 0; i < Model.Count; i++)
{
#Html.HiddenFor(model => Model[i].Inscrição)
#Html.HiddenFor(model => Model[i].Nome)
#Html.HiddenFor(model => Model[i].NotaPrimeiraFase)
<tr>
<td>#Model[i].Inscrição</td>
<td>#Model[i].Nome</td>
<td>#Model[i].NotaPrimeiraFase</td>
#*do this similary for other property *#
</tr>
}
</tbody>
</table>
<button type="submit" class="btn btn-success">Salvar</button>
}
As far as I understood,you want to post DropDownList value only.
if you decide to update other fields just add the Html.Editor into the loop like following.
#Html.Editor("[" + i + "].Inscricao")
#Html.Editor("[" + i + "].Nome")
I hope this will help.
#using (Html.BeginForm("UdateNotas", "Home", Model, FormMethod.Post))
{
<table class="table table-bordered" id="tblNotaAprovadosSegundaFase">
<thead>
<tr>
<th>Inscrição</th>
<th>Nome</th>
<th>Nota Primeira Fase</th>
<th>Fez Carta</th>
<th>Nota Segunda Fase</th>
</tr>
</thead>
<tbody>
#if (Model != null)
{
#for (int i = 0; i < Model.Count; i++)
{
<tr>
<td>#Model[i].Inscrição</td>
<td>#Model[i].Nome</td>
<td>#Model[i].NotaPrimeiraFase</td>
<td>
<select name="[#i].NotaSegundaFase">
<option value="false">Não</option>
<option value="true">Sim</option>
</select>
</td>
<td><input type="text" value="#linha.NotaSegundaFase"></td>
</tr>
}
}
</tbody>
</table>
<button type="submit" class="btn btn-success">Salvar</button>
}
Note : don't change anything in the controller. model binder should resolve everything from the request.

How to bind the multiple radio button values to controller action

following is my view code, I pass list from controller to view and here I am iterating that list through foreach I want to pass the same view-model list to the action when I submit the form
#model IEnumerable<AskQuestionViewModel>
.
.
.
<table class="table table-bordered">
<thead>
<tr>
<td>Question</td>
<td>Yes</td>
<td>No</td>
</tr>
</thead>
<tbody>
#foreach (var q in Model)
{
<tr>
<td>#q.Question</td>
#if (q.Answer == true)
{
<td><input type="radio" name="#q.QuestionID" value="true" checked />/td>
}
else
{
<td><input type="radio" name=#q.QuestionID value="true" required /></td>
}
#if (q.Answer == false)
{
<td><input type="radio" name=#q.QuestionID value="false" checked /></td>
}
else
{
<td><input type="radio" name=#q.QuestionID value="false" required /></td>
}
</tr>
}
The following is action code
[HttpPost]
public ActionResult AskQuestions(List<AskQuestionViewModel> askQVM)
{
...
}
I'm getting null in askQVM
This is what you need for postback.
<table class="table table-bordered">
<thead>
<tr>
<td>Question</td>
<td>Yes</td>
<td>No</td>
</tr>
</thead>
<tbody>
#using (Html.BeginForm("AskQuestions", "Questions", FormMethod.Post))
{
for (int i = 0; i < Model.Count; i++)
{
var yesOptions = Model[i].Answer ? new { #checked = "checked" } : null;
var noOptions = Model[i].Answer ? null : new { #checked = "checked" };
<tr>
<td>
#Model[i].Question
#Html.HiddenFor(model=>Model[i].QuestionID)
#Html.HiddenFor(model=>Model[i].Question)
</td>
<td>#Html.RadioButtonFor(model => Model[i].Answer, true, yesOptions)</td>
<td>#Html.RadioButtonFor(model => Model[i].Answer, false, noOptions)</td>
</tr>
}
<button type="submit">Submit</button>
}
</tbody>
</table>

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

Passing a list from Controller to View in ASP.NET MVC 4

I have an Index action method as follows.I am passing a list of Providers to the View.
public ActionResult Index()
{
Provider providerList = new Provider();
List<Provider> providers = DAL.GetListofProviders.ToList();
return View(providers);
}
In the View,I have the following code to receive the List of Providers.
#model IEnumerable<DEMO_JAN14.Models.Provider>
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<head>
<title>LIST OF PROVIDERS</title>
</head>
<body>
<table class="table table-striped table-bordered table-hover">
<tr>
<th>Provider Type</th>
<th>First Name</th>
<th>Last Name</th>
<th>Certification</th>
<th>Specialization</th>
<th>SSN</th>
<th>Facility Name</th>
<th>Contact No</th>
<th>Contact Email</th>
<th></th>
</tr>
<tbody data-bind="foreach: viewmodel">
<tr>
<td class="col-lg-2" data-bind="text: ProviderType"></td>
<td class="col-lg-1" data-bind="text: FirstName"></td>
<td class="col-lg-1" data-bind="text: LastName"></td>
<td class="col-lg-1" data-bind="text: Certification"></>
<td class="col-lg-1" data-bind="text: Specialization"></td>
<td class="col-lg-1" data-bind="text: SSN"></td>
<td class="col-lg-4" data-bind="text: FacilityName"></td>
<td class="col-lg-4" data-bind="text: ContactNumber"></td>
<td class="col-lg-1" data-bind="text: ContactEmail"></td>
<td><a class="btn btn-danger" id="del" onclick = "return confirm('Are you sure, you want to delete');" data-bind="attr: { href: '/Provider/Delete/' + ProviderID }"> Delete </a>
</td>
</tr>
</tbody>
I see the list of Providers in the controller
But I dont see the same list in the view as shown
Am I doing something wrong.Please guide me in the right directions.Thanks.
You can iterate like this (using your code)
#model IEnumerable<DEMO_JAN14.Models.Provider>
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<head>
<title>LIST OF PROVIDERS</title>
</head>
<body>
<table class="table table-striped table-bordered table-hover">
<tr>
<th>#Html.DisplayNameFor(m => m.ProviderType)</th>
<th>#Html.DisplayNameFor(m => m.FirstName)</th>
...
</tr>
#foreach (var item in Model)
<tr>
<td class="col-lg-2">#Html.DisplayFor(modelItem => item.ProviderType)</td>
<td class="col-lg-1">#Html.DisplayFor(modelItem => item.FirstName)</td>
...
</tr>
</table>
</body>
To check the count of the providers list inside the view use the following property #Model
In your view you can iterate like this on the items, and for example am displaying FirstName of each Provider in the List:
#foreach(var item in Model)
{
<h1>#item.FirstName </h1>
}
Using your html:
<tbody data-bind="foreach: viewmodel">
#foreach(var item in Model)
{
<tr>
<td class="col-lg-2">#item.ProviderType</td>
<td class="col-lg-1">#item.FirstName</td>
........................................
........................................
// and so on other properties
<td><a class="btn btn-danger" id="del" onclick = "return confirm('Are you sure, you want to delete');" href="#Url.Action("Delete","Provider")+ item.ProviderID }"> Delete </a>
</td>
</tr>
you can't access the razor Model object via knockout
you need to receive your data as json result
please refer to this article
http://www.codeproject.com/Articles/424642/Customer-KnockoutJS-and-MVC-demo-using-JSON

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