Not showing partial view result inside table body in asp.net MVC - asp.net-mvc

I have written code to implement search using ajax in asp.net MVC. I have written an action inside my controller (Admincontroller) using partial view which is supposed to fetch data. I want to display the partial view result inside table body in my Index page.
The search function is working fine, but it is not displaying the result inside table body with id "searchresult" (Inside Index page). instead of that it is giving result. I wanted the result from search in table format. now result is displayed like below in http://localhost:1274/Admin/Search.
2786 Mens L/S 4000
here is my Index.cshtml
#model IEnumerable<TestEntityFramework.Models.trProducts>
#{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_AdminLayout.cshtml";
AjaxOptions ajax = new AjaxOptions
{
UpdateTargetId = "searchresult",
Confirm = "Are you sure to start search?",
InsertionMode = InsertionMode.Replace,
LoadingElementId="Loadhere"
};
}
<h2>Index</h2>
<script src="~/scripts/jquery.unobtrusive-ajax.js"></script>
<script src="~/scripts/jquery.unobtrusive-ajax.min.js"></script>
<div class="panel-heading">
<div>
#Html.ActionLink("Create New", "Create",null, new{ #class="btn btn- primary" })
</div>
</div>
<div class="panel-body">
<table class="table table-striped table-bordered table-condensed table- hover table-responsive">
<tr>
<th>
#Html.DisplayNameFor(model => model.prod_type)
</th>
<th>
#Html.DisplayNameFor(model => model.prod_name)
</th>
<th>
#Html.DisplayNameFor(model => model.prod_desc)
</th>
<th>
#Html.DisplayNameFor(model => model.prod_price)
</th>
<th>
#Html.DisplayNameFor(model => model.prod_qty)
</th>
<th>
#Html.DisplayNameFor(model => model.prod_category)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.prod_type)
</td>
<td>
#Html.DisplayFor(modelItem => item.prod_name)
</td>
<td>
#Html.DisplayFor(modelItem => item.prod_desc)
</td>
<td>
#Html.DisplayFor(modelItem => item.prod_price)
</td>
<td>
#Html.DisplayFor(modelItem => item.prod_qty)
</td>
<td>
#Html.DisplayFor(modelItem => item.prod_category)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new {id=item.product_id}) |
#Html.ActionLink("Details", "Details", new { id=item.product_id }) |
#Html.ActionLink("Delete", "Delete", new { id=item.product_id })
</td>
</tr>
}
</table>
</div>
<div class="col-lg-8">
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>
Product Id
</th>
<th>
Product Name
</th>
<th>
Price
</th>
<th>
</th>
</tr>
</thead>
<tbody id="searchresult">
#Html.Action("Search", new { pr_name = "" })
</tbody>
</table>
#using (Ajax.BeginForm("Search", ajax))
{
<div id="Loadhere">
#Html.TextBox("pr_name")
<button class="btn btn-primary">Search</button>
</div>
}
</div>
and my partialview Search.cshtml looks as below
#model IEnumerable<TestEntityFramework.Models.trProducts>
#foreach(var m in Model)
{
<tr>
<td>
#m.product_id
</td>
<td>
#m.prod_name
</td>
<td>
#m.prod_price
</td>
</tr>
}

Related

Is there a way I can add and bind checkboxes to my MVC Table

I am trying to add a checkbox so I can selects a row from my list and from there use that row to generate a letter.
I was wondering how I would be able to add a checkbox and bind it to the data I have tried a few ways using something similar to:
checkbox(m => m[i].Lesson)
However it would never accept the i in brackets and come up with an error. below is the layout of my index for the lessons model currently.
Here is the Lessons Index:
#model IEnumerable<FinalMusicApp.Models.Lesson>
#{
ViewBag.Title = "Index";
}
<h2>Lessons</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
#using (Html.BeginForm("Index", "Students", FormMethod.Get))
{
//Search options
<b>Search Options: </b> #Html.RadioButton("Option", "Instrument")<text>Instrument</text>#Html.RadioButton("Option", "Tutor")<text>Tutor</text>#Html.RadioButton("Option", "Term")<text>Term</text>#Html.TextBox("Search")
<input type="submit" name="submit" value="Search" />
}
<form method="post">
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.IsChecked)
</th>
<th>
#Html.DisplayNameFor(model => model.LessonDate)
</th>
<th>
#Html.DisplayNameFor(model => model.LessonTime)
</th>
<th>
#Html.DisplayNameFor(model => model.Duration.TimeTaken)
</th>
<th>
#Html.DisplayNameFor(model => model.Instrument.InstrumentName)
</th>
<th>
#Html.DisplayNameFor(model => model.Letter.Paid)
</th>
<th>
#Html.DisplayNameFor(model => model.Student.FullName)
</th>
<th>
#Html.DisplayNameFor(model => model.Tutor.TutorName)
</th>
<th>
CRUD
</th>
<th>
Make a letter
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.CheckBoxFor(modelItem => item.IsChecked)
</td>
<td>
#Html.DisplayFor(modelItem => item.LessonDate)
</td>
<td>
#Html.DisplayFor(modelItem => item.LessonTime)
</td>
<td>
#Html.DisplayFor(modelItem => item.Duration.TimeTaken)
</td>
<td>
#Html.DisplayFor(modelItem => item.Instrument.InstrumentName)
</td>
<td>
#Html.DisplayFor(modelItem => item.Letter.Paid)
</td>
<td>
#Html.DisplayFor(modelItem => item.Student.FullName)
</td>
<td>
#Html.DisplayFor(modelItem => item.Tutor.TutorName)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id = item.LessonId }) |
#Html.ActionLink("Details", "Details", new { id = item.LessonId }) |
#Html.ActionLink("Delete", "Delete", new { id = item.LessonId })
</td>
<td>
#Html.ActionLink("Generate Letter", "Create", "Letters")
</td>
</tr>
}
</table>
</form>
I am trying to add a checkbox so I can selects a row from my list
Do you want something like below?
In the view add a checkbox, for example I use the property CourseName in my model instead, you can use your property:
#Html.CheckBox("CourseName", new { value = item.CourseName })
#Html.DisplayFor(modelItem => item.CourseName)
Then in the controller, we can get CourseName value, and get other property value accord to the CourseName, but we need load the list again, we cannot send the full list from view to controller :
var courserow = model.FirstOrDefault(m => m.CourseName == CourseName);
result:

How to render a table from a view in the footer section of _Layout view?

I am developing a solution to enter project-split times in the fullcalendar jquery application. A SQL view shows incomplete days (where entered time is not equal to 8 hours) and renders the table from the entity model in the Generate view:
#model IEnumerable<support_tickets.Models.DailyHours>
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Generate</title>
</head>
<body>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.Start)
</th>
<th>
#Html.DisplayNameFor(model => model.Week)
</th>
<th>
#Html.DisplayNameFor(model => model.Hours)
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Start)
</td>
<td>
#Html.DisplayFor(modelItem => item.Week)
</td>
<td>
#Html.DisplayFor(modelItem => item.Hours)
</td>
</tr>
}
</table>
</body>
</html>
I'd like to render the same table in the footer section of the _Layout view, to see everything on one page. Any assistance is greatly appreciated. Thanks
This is what worked for me - creating a partial view from the PartialViewResult action.
In Controller:
public PartialViewResult IncompleteDays()
{
ticketsEntities entities = new ticketsEntities();
var ent = entities.DailyHours.ToList();
return PartialView(ent);
}
Create a partial View from the action, with the table:
#model IEnumerable<support_tickets.Models.DailyHours>
<div class="row justify-content-center">
<div class="col-auto">
<table class="table table-striped">
<tr>
<th>
#Html.DisplayNameFor(model => model.Start)
</th>
<th>
#Html.DisplayNameFor(model => model.Week)
</th>
<th>
#Html.DisplayNameFor(model => model.Hours)
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Convert.ToDateTime(item.Start).ToString("MM-dd-yyyy")
</td>
<td>
#Html.DisplayFor(modelItem => item.Week)
</td>
<td>
#Html.DisplayFor(modelItem => item.Hours)
</td>
</tr>
}
</table>
</div>
</div>
And then call the action from the footer section of the _Layout view:
#{ Html.RenderAction("IncompleteDays", "Home");}

Object reference not set to an instance of an object. First login. solved when I rerun the application?

When I create a user and he's logged in for the first time I get this error when I try to display(View details) the requests that he created so far(0 request created). Once I rerun the application again with the same user the view works and I get no error. Here's the line that's generates the error:
#foreach (var item in Model)`
Here's the view that generates the error:
#model IEnumerable<Mutuelle.Domain.Entities.Demande>
#{
ViewBag.Title = "DemandesAdherent";
}
#{
Layout = "~/Views/Shared/_LayoutAdherent.cshtml";
}
<h2>DemandesAdherent</h2>
<p>
#Html.ActionLink("CreƩr nouvelle", "AddDemande")
</p>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.Date)
</th>
<th>
#Html.DisplayNameFor(model => model.nomBeneficiaire)
</th>
<th>
#Html.DisplayNameFor(model => model.Nomrubrique)
</th>
<th>
#Html.DisplayNameFor(model => model.etat)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Date)
</td>
<td>
#Html.DisplayFor(modelItem => item.nomBeneficiaire)
</td>
<td>
#Html.DisplayFor(modelItem => item.Nomrubrique)
</td>
<td>
#Html.DisplayFor(modelItem => item.etat)
</td>
<td>
#Html.ActionLink("Details", "DetailsDemande", new { id=item.DemandeId }) #*|
#Html.ActionLink("Delete", "DeleteDemande", new { id=item.DemandeId })*#
</td>
</tr>
}
</table>

How to Delete Multiple Record using Checkbox?

I'm creating a project for my institute. Using Asp.net MVC, I need multiple delete option with selected checkbox.
I have added a check in my view, but not delete multiple Raw. I don't want to use third party plugin. Please help me.
<table class="table table-striped table-condensed table-bordered">
<tr>
<th>
Select
</th>
<th>
#Html.ActionLink("Book Id", "Index", new { sortOrder = ViewBag.IdSortParm, currentFilter = ViewBag.CurrentFilter })
</th>
<th>
#Html.ActionLink("Title", "Index", new { sortOrder = ViewBag.NameSortParm, currentFilter = ViewBag.CurrentFilter })
</th>
<th>
#Html.ActionLink("Date", "Index", new { sortOrder = ViewBag.DateSortParm, currentFilter = ViewBag.CurrentFilter })
</th>
<th>
Price
</th>
<th>
Category
</th>
<th class="text-center">
Photo
</th>
<th>
User
</th>
<th>Edit</th>
<th>Delete</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
<input type="checkbox" name="deleteInputs" value="#item.BookId" />
</td>
<td>
#Html.DisplayFor(modelItem => item.BookId)
</td>
<td>
#Html.ActionLink(item.BookTitle, "Details", new
{
id = item.BookId
})
</td>
<td>
#Html.DisplayFor(modelItem => item.PublishDate)
</td>
<td>
#Html.DisplayFor(modelItem => item.Price)
</td>
<td>
#Html.DisplayFor(modelItem => item.Category)
</td>
<td class="text-center">
<img class="img-thumbnail" width="50" height="50" src="~/ContentImages/Full/#item.Photo" />
</td>
<td>
#Html.DisplayFor(modelItem => item.UserName)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id = item.BookId })
</td>
<td>
#Html.ActionLink("Delete", "Delete", new { id = item.BookId })
</td>
</tr>
}
</table>
This could be a way to achieve it:
First embed the table within a named form, that executes the batch delete action, like so:
#{ Html.BeginForm("BatchDelete", "Book", FormMethod.Post, new { name = "tableForm" }); }
<table class="table table-striped table-condensed table-bordered">
<tr>
<th>
Select
</th>
<th>
#Html.ActionLink("Book Id", "Index", new { sortOrder = ViewBag.IdSortParm, currentFilter = ViewBag.CurrentFilter })
</th>
<th>
#Html.ActionLink("Title", "Index", new { sortOrder = ViewBag.NameSortParm, currentFilter = ViewBag.CurrentFilter })
</th>
<th>
#Html.ActionLink("Date", "Index", new { sortOrder = ViewBag.DateSortParm, currentFilter = ViewBag.CurrentFilter })
</th>
<th>
Price
</th>
<th>
Category
</th>
<th class="text-center">
Photo
</th>
<th>
User
</th>
<th>Edit</th>
<th>Delete</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
<input type="checkbox" name="deleteInputs" value="#item.BookId" />
</td>
<td>
#Html.DisplayFor(modelItem => item.BookId)
</td>
<td>
#Html.ActionLink(item.BookTitle, "Details", new
{
id = item.BookId
})
</td>
<td>
#Html.DisplayFor(modelItem => item.PublishDate)
</td>
<td>
#Html.DisplayFor(modelItem => item.Price)
</td>
<td>
#Html.DisplayFor(modelItem => item.Category)
</td>
<td class="text-center">
<img class="img-thumbnail" width="50" height="50" src="~/ContentImages/Full/#item.Photo" />
</td>
<td>
#Html.DisplayFor(modelItem => item.UserName)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id = item.BookId })
</td>
<td>
#Html.ActionLink("Delete", "Delete", new { id = item.BookId })
</td>
</tr>
}
</table>
<!-- Section for buttons -->
<div class="actions">
<a href="javascript:(function(){document.tableForm.submit();return void(0);})()">
Delete selected books
</a>
</div>
#{ Html.EndForm(); }
Note that after the table, before ending the form, I added a link that executes the submit of the form.
Now, on the controller side, asumming its name is "BookController":
public class BookController : Controller
{
// ...
[HttpPost]
public ActionResult BatchDelete(int[] deleteInputs)
{
// You have your books IDs on the deleteInputs array
if (deleteInputs != null && deleteInputs.Length > 0)
{
// If there is any book to delete
// Perform your delete in the database or datasource HERE
}
// And finally, redirect to the action that lists the books
// (let's assume it's Index)
return RedirectToAction("Index");
}
// ...
}
Notice that:
The first two parameter of the Html.BeginForm method, are the action name and controller name (without the "Controller" suffix).
The last parameter of the same method include the name of the form. The name is used in the javascript of the link, in order to indicate which form are you going to submit.
First, you need a different Id for each checkbox so you know which record is to be deleted. Second, if you implement "delete" as a link then the browser will perform a GET action instead of a POST. Assuming you are not using AJAX then you will need a form so you can POST to the controller action which handles the delete.

How can I insert mulitple rows in table at once in MVC asp .net?

I am new at MVC asp .net.
I have a grid in which I am adding rows.
Now I want to perform mulitple rows insertion in a table at once.
How can I do that. SO multiple rows will be added in MVC.
Any help would be appericiated.
You want to learn how to ModelBind to a Collection. There are many answers here already for this technique.
https://stackoverflow.com/search?q=model+binding+to+a+collection
http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
Do you mean something like this?.
<table class="table" style="vertical-align: middle;">
<thead>
<tr>
<th>
#Html.DisplayNameFor(model => model.title)
</th>
<th style="text-align: center;">
#Html.DisplayNameFor(model => model.imgAr)
</th>
<th>
#Html.DisplayNameFor(model => model.created)
</th>
<th></th>
</tr>
</thead>
<tbody id="tblOfContent">
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.title)
</td>
<td style="text-align: center;">
<img src="~/newsImages/#item.imgAr" class="img-thumbnail" />
</td>
<td>
#item.created.ToShortDateString()
</td>
<td>
#Html.ActionLink("Details", "Edit", new { id = item.id }) |
#Html.ActionLink("Details", "Details", new { id = item.id })
</td>
</tr>
}
</tbody>
</table>
and in the Controller something like this:
public ActionResult Index()
{
return View(newsService.GetAll().OrderByDescending(x => x.created));
}

Resources