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

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

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 put a row in red with Razor in MVC5

I have a table, which I want to mark when the real stock is less than the minimum stock, for that I'm going to render the view using Razor, but I'm not getting results
I enclose my view that contains the table that we wish to intervene
<table class="table table-bordered table-hover table-condensed">
<tr>
<th>
#Html.DisplayNameFor(model => model.First().v_Nombre)
</th>
<th>
#Html.DisplayNameFor(model => model.First().StockReal)
</th>
<th>
#Html.DisplayNameFor(model => model.First().StockMinimo)
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
var fila = string.Empty;
if(item.StockReal < item.StockMinimo)
{
fila = "danger";
}
<tr class="#fila">
<td>
#Html.DisplayFor(modelItem => item.v_Nombre)
</td>
<td>
#Html.DisplayFor(modelItem => item.StockReal)
</td>
<td>
#Html.DisplayFor(modelItem => item.StockMinimo)
</td>
<td>
Editar
</td>
</tr>
}
</table>
Expected behavior: that the row where the real stock is less than the minimum stock turns red
Behavior Achieved: No change in my view
what am I doing wrong? What is missing in my Razor code? any help for me?
In bootstrap 4 the class is table-danger so change your code:
if(item.StockReal < item.StockMinimo)
{
fila = "table-danger";
}

Display a single item from the Model in the same view as a list

I want to display a search result using this InstitutionName as a query, but instead of appearing in the resulting table repeatedly on every row, I just want to show it as a heading. I tried using DisplayFor but it always returns an error, I'm guessing because my model is an IEnumerable, but I don't know, I just put in DisplayNameFor temporarily to simulate the output I want to show, which should be the value of the property itself and not just the property name.
I've read about using a ViewModel but I just want to know if I can accomplish this with what I have right now.
View
#model IEnumerable<SMOSystemv2.Models.Tender>
#{
ViewBag.Title = "Tenders";
}
<h1>Tender History</h1>
<h2>#Html.DisplayNameFor(model => model.Compendium.Institution.InstitutionName)</h2>
<table class="table">
<tr>
<th nowrap>
#Html.DisplayNameFor(model => model.Compendium.Institution.InstitutionName)
</th>
<th nowrap>
#Html.DisplayNameFor(model => model.Compendium.Qualification.Title)
</th>
<th nowrap>
#Html.DisplayNameFor(model => model.Slots)
</th>
<th nowrap>
#Html.DisplayNameFor(model => model.TotalAmount)
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td nowrap>
#Html.DisplayFor(modelItem => item.Compendium.Institution.InstitutionName)
</td>
<td nowrap>
#Html.DisplayFor(modelItem => item.Compendium.Qualification.Title)
</td>
<td nowrap>
#Html.DisplayFor(modelItem => item.Slots)
</td>
<td nowrap>
#Html.DisplayFor(modelItem => item.TotalAmount)
</td>
</tr>
}
</table>
This is the kind of display that I was initially looking for using #Model.First() as suggested, but implementing a ViewModel instead is highly recommended
#model IEnumerable<SMOSystemv2.Models.Tender>
#{
ViewBag.Title = "Tenders";
}
<h1>Tender History</h1>
<h2>#Model.First().Compendium.Institution.InstitutionName</h2>
<table class="table">
<tr>
<th nowrap>
#Html.DisplayNameFor(model => model.Compendium.Qualification.Title)
</th>
<th nowrap>
#Html.DisplayNameFor(model => model.Slots)
</th>
<th nowrap>
#Html.DisplayNameFor(model => model.TotalAmount)
</th>
<th nowrap>
#Html.DisplayNameFor(model => model.Semester)
</th>
<th nowrap>
#Html.DisplayNameFor(model => model.DateReceived)
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
<tr>
<td nowrap>
#Html.DisplayFor(modelItem => item.Compendium.Qualification.Title)
</td>
<td nowrap>
#Html.DisplayFor(modelItem => item.Slots)
</td>
<td nowrap>
#Html.DisplayFor(modelItem => item.TotalAmount)
</td>
<td nowrap>
#Html.DisplayFor(modelItem => item.Semester)
</td>
<td nowrap>
#Html.DisplayFor(modelItem => item.DateReceived)
</td>
<td nowrap>
#Html.ActionLink("Edit", "Edit", new { id = item.ID }) |
#Html.ActionLink("Details", "Details", new { id = item.ID }) |
#Html.ActionLink("Delete", "Delete", new { id = item.ID })
</td>
</tr>
}
</table>
This property returns the first option of the list or option selected as the default
FirstOrDefault
#if(Model != null){
<h2>#Model.FirstOrDefault().Compendium.Institution.InstitutionName</h2>
}
Try following solution:
<h2>#Html.DisplayNameFor(#Model.FirstOrDefault().Compendium.Institution.InstitutionName)</h2>
instead of
<h2>#Html.DisplayNameFor(model=>model.Compendium.Institution.InstitutionName)</h2>
because you are using #model IEnumerable<SMOSystemv2.Models.Tender> at your code. The property FirstOrDefault() will give you the first value in the list as default value.

Not showing partial view result inside table body in 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>
}

Templates can be used only with field access, property access in Razor

I got this error when I was creating a Edit and Templating for Product.
Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.
Edit In my Razor View:
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
#Html.DisplayNameFor(model => model.Select(m => m.ProductID))
</th>
<th>
#Html.DisplayFor(model => model.Select(m => m.Name))
</th>
<th>
#Html.DisplayFor(model => model.Select(m => m.ListPrice))
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.ProductID)
</td>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.ListPrice)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id=item.ProductID }) |
#Html.ActionLink("Details", "Details", new { id=item.ProductID }) |
#Html.ActionLink("Delete", "Delete", new { id=item.ProductID })
</td>
</tr>
}
</table>
Do you know what that mean and how to fix it? Thank you so much!
doesnt this just work?
<th> #Html.DisplayFor(model => model.ProductID) </th>
edit, try something like this:
<table id="myTable">
<tr>
<th>
#Html.DisplayNameFor(model => model.ProductID)
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.ProductID)
</td>
<td>
#Html.DisplayFor(modelItem => item.ProductID)
</td>
</tr>
}
</table>

Resources