Change the string from database column while I displays over html - c#

This is how i am set my page according my database column:
This is the code (only for the first column)
<table class="table">
<tr>
<th style="font-size: 20px">
#Html.DisplayNameFor(model => model.fileName)
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
<tr>
<td style="font-size: 15px">
#Html.DisplayFor(modelItem => item.fileName)
</td>
<td>
#Html.ActionLink("File Details", "Details", new { id = item.id })
</td>
</tr>
}
</table>
How can i change fileName to another string when over my html page ?

I don't know if this will work(I am unable to test it at the moment, not on windows),
[Display(Name = "File Name")] //this is the line you must add.
public string fileName{get;set;} //this is your model property name.
This should change it to File Name

Related

where can i found search input textbox on controller action or view?

I working on asp.net mvc application by csharp . i face issue I can't find controls search input type textbox .
I search on action controller PendingRequests i can't found it .
also i search on index.html it not exist
so How to get search text box please ?
action controller
public ActionResult Index(string filenumber)
{
int employeeFileNo;
string userRole;
if (!string.IsNullOrEmpty(filenumber))
{
if (int.TryParse(filenumber, out employeeFileNo))
{
JDEUtility jde = new JDEUtility();
userRole = Workforce.GetUserRole(employeeFileNo);
if (!string.IsNullOrEmpty(userRole))
{
var EmpName = jde.GetEmployeeName(employeeFileNo);
Session[SessionKeys.UserCode] = employeeFileNo;
Session[SessionKeys.Username] = EmpName;
Session[SessionKeys.RoleCode] = userRole;
if (userRole != RoleKey.REQ)
return RedirectToAction("PendingRequests", "WorkforceRequests", null);
else
return RedirectToAction("MyRequests", "WorkforceRequests", null);
}
else
ViewBag.errorMsg = "unauthorized user";
}
else
{
ViewBag.errorMsg = "incorrect file no";
}
}
else
{
ViewBag.errorMsg = "unauthorized user";
}
return View();
}
pending request view
#model IEnumerable<HR.WorkforceRequisition.Models.WorkforceRequest>
#{
ViewBag.Title = "Pending Requests";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Pending Requests</h2>
<hr />
#if (!string.IsNullOrWhiteSpace(ViewBag.msg))
{
<div class="alert alert-success">
#ViewBag.msg
</div>
}
#if (!string.IsNullOrWhiteSpace(ViewBag.errorMsg))
{
<div class="alert alert-danger">
#ViewBag.errorMsg
</div>
}
<table id="dtbl" class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th></th>
<th>
#Html.DisplayNameFor(model => model.WorkforceRequestID)
</th>
<th>
#Html.DisplayNameFor(model => model.DepartmentCode)
</th>
<th>
#Html.DisplayNameFor(model => model.Section)
</th>
<th>
#Html.DisplayNameFor(model => model.RequiredPosition)
</th>
<th>
#Html.DisplayNameFor(model => model.JobTitle)
</th>
<th>
#Html.DisplayNameFor(model => model.ReportingTo)
</th>
<th>
#Html.DisplayNameFor(model => model.NationalityCategory)
</th>
<th>
#Html.DisplayNameFor(model => model.StatusRemark)
</th>
<th>
Requestor
</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#Html.ActionLink("Details", "Details", new { id = item.WorkforceRequestID })
</td>
<td>
#Html.DisplayFor(modelItem => item.WorkforceRequestID)
</td>
<td>
#Html.DisplayFor(modelItem => item.DepartmentCode)
</td>
<td>
#Html.DisplayFor(modelItem => item.Section)
</td>
<td>
#Html.DisplayFor(modelItem => item.RequiredPosition)
</td>
<td>
#Html.DisplayFor(modelItem => item.JobTitle)
</td>
<td>
#Html.DisplayFor(modelItem => item.ReportingTo)
</td>
<td>
#Html.DisplayFor(modelItem => item.NationalityCategory)
</td>
<td>
#Html.DisplayFor(modelItem => item.StatusRemark)
</td>
<td>
#Html.DisplayFor(modelItem => item.CreatedByName)
</td>
</tr>
}
</tbody>
</table>
<script>
$(document).ready(function () {
$('#dtbl').DataTable({
"scrollX": true,
"pageLength": 10,
dom: 'Bfrtip',
buttons: [
'copy', 'csv', 'excel'
]
});
});
</script>
expected result is found search text box input on which place
display text box on pending requests
From the screenshot, it seems you need search text box in the data table. The search box appears by default as such unless you have set any property to not show it. With your js I am able to see the search box You can try adding "searching": true property to your data table also. You can check the documentation here.
Here is a working snippet
$(document).ready(function () {
$('#tableID').DataTable({
// Enable the searching
// of the DataTable
searching: true // will work without this also
});
});
<!-- jQuery -->
<script type="text/javascript"
src="https://code.jquery.com/jquery-3.5.1.js">
</script>
<!-- DataTables CSS -->
<link rel="stylesheet" href=
"https://cdn.datatables.net/1.10.23/css/jquery.dataTables.min.css">
<!-- DataTables JS -->
<script src=
"https://cdn.datatables.net/1.10.23/js/jquery.dataTables.min.js">
</script>
<table id="tableID" class="display">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Samer</td>
<td>35</td>
</tr>
<tr>
<td>2</td>
<td>Rajiv</td>
<td>30</td>
</tr>
<tr>
<td>3</td>
<td>Suhail</td>
<td>45</td>
</tr>
<tr>
<td>4</td>
<td>Manisha</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
<td>Robbert</td>
<td>68</td>
</tr>
<tr>
<td>6</td>
<td>Mike</td>
<td>25</td>
</tr>
<tr>
<td>7</td>
<td>Ere</td>
<td>23</td>
</tr>
<tr>
<td>8</td>
<td>James</td>
<td>35</td>
</tr>
<tr>
<td>9</td>
<td>Walt</td>
<td>65</td>
</tr>
<tr>
<td>10</td>
<td>Jessieca</td>
<td>28</td>
</tr>
<tr>
<td>11</td>
<td>Raghu</td>
<td>20</td>
</tr>
</tbody>
</table>

Loop through Table, look for checked checkbox, add to database?

I've been searching for an article or example but can't seem to find what I'm looking for. If I write out a table in a View with a checkbox in one of the columns, how would I go about looping through the row, seeing if the checkbox is checked, and if it is then add the record to a database?
I'm still learning about checkboxes and dropdownlists in MVC so I will eventually have to figure out how I would assign the UserID to the row or checkbox and then I'll look at a dropdownlist for the GroupId to assign them to.
The basic scenario is to have a list of unassigned users with a dropdownlist of Groups above the table. The Admin will then make a selection of which group from the DDL and then check the users he wants to assign to it. Then I need to loop through get the UserId for the records that were checked and add them to a database with EntityFramework.
Anyone know of any examples or articles for something similar?
<table class="table table-bordered table-striped table-hover table-condensed tbackground">
<tr>
<th class="text-center">
</th>
<th class="text-center">
First Name
</th>
<th class="text-center">
Last Name
</th>
<th class="text-center">
Email
</th>
<th class="text-center">
Phone
</th>
</tr>
#foreach (var item in Model)
{
if (item.GroupId == 0)
{
<tr>
<td>
#Html.CheckBox("isSelected")
</td>
<td>
#Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
#Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
#item.EmailPrimary
</td>
<td class="text-center">
#Html.DisplayFor(modelItem => item.PhonePrimary)
</td>
</tr>
}
}
</table>
You can do it in a couple ways:
If you wanna check everything and post the form that contains your table
Add IsSelected property to your model and change your foreach to a for loop. This way your table for will look like this:
#for (var i = 0; i < Model.Count(); i++)
{
if (item.GroupId == 0)
{
<tr>
<td>
#Html.CheckBoxFor(x => x[i].IsSelected)
</td>
<td>
#Html.DisplayFor(x => x[i].FirstName)
</td>
<td>
#Html.DisplayFor(x => x[i].LastName)
</td>
<td>
#Model[i].EmailPrimary
</td>
<td class="text-center">
#Html.DisplayFor(x => x[i].PhonePrimary)
</td>
</tr>
}
}
Using index allows your ModelBinder to know that you're posting a list and will bind the properties of this list. That is, if a checkbox is checked, you'll get a true value for your IsSelected property in Controller.
Then, you can do this in Controller to get the selected records:
var selecteds = MyPostedModel.Where(x => x.IsSelected);
or...
If you want to check and save then
First add an data-* property to the checkbox which will hold your Id:
#Html.CheckBox("isSelected", false, new { data_group_id = item.GroupId})
Then add an event listner to your checkbox click with jquery:
$("input[name='isSelected']").on('click', function() {
var groupId = $(this).data("group-id");
var isChecked = $(this).is(":checked");
$ajax.... //Use an ajax call and pass the values to your action
});

MVC Partial View not rendering

Partial view is not rendering while passing ViewModel.. Its rendering without ViewModel.
I mean if I keep #Html.Partial("PartialClientIndex") then its rendering and when I pass ViewModel it directly going to Dispose without rendering partial view.
I'm missing something here.. could you please help in this.
Main View:
<div id="PartialClient">
#Html.Partial("PartialClientIndex", viewModelList)
</div>
Action:
[HttpPost]
public ActionResult PartialClientIndex(int? SelectedGroupId)
{
int SkipRec = 0;
int NextRec = 25;
VMClient vmclient = new VMClient();
vmclient.IEClients = db.client.Where(cl => cl.Groups.id == SelectedGroupId).OrderBy(c => c.id).Skip(SkipRec).Take(NextRec).ToList();
return PartialView("PartialClientIndex", vmclient);
}
Partial View:
#model IEnumerable<HostingManager.Models.VMClient>
<table>
<thead>
<tr>
<th style="width:25px; padding:10px;">
Group
</th>
<th class="tbContent-Name">
Client Name
</th>
<th class="tbContent-Name">
Contact Person
</th>
<th class="tbContent-Name">
Contact Email
</th >
<th></th>
</tr>
</thead>
<tbody>
#if(Model != null)
{
var x = Model.Select(c=>c.IEClients).ToList();
var y = x[0].ToList();
// var y = x[0]["item1"];
foreach (var item in y) {
<tr>
<td class="tbContent-Name">
#Html.DisplayFor(modelItem => item.Groups.name)
</td>
<td class="tbContent-Name">
#Html.DisplayFor(modelItem => item.contactPerson)
</td>
<td class="tbContent-Name">
#Html.DisplayFor(modelItem => item.contactPerson)
</td>
<td class="tbContent-Name">
#Html.DisplayFor(modelItem => item.contactEmail)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id=item.id }) |
#Html.ActionLink("Delete", "Delete", new { id = item.id }, new { onclick = "return confirm('Are you sure you wish to delete this ?');" })
</td>
</tr>
}
}
</tbody>
It seems that the variable vmclient that you are passing as model into your partial view is of type VMClient. Though your partial view is expecting the IEnumerable<VMClient> type.
You basically have to change the model type in your partial view to the following
#model HostingManager.Models.VMClient
and adjust the way you are assigning the y variable
var y = Model.IEClients;

add edit table, how to change a row to edit mode?

Below is my razor code for my table of users and the current add/edit layout
<table class="smalltable" cellpadding="0" cellspacing="0" id="tblUsers">
<thead>
<tr>
<th> </th>
<th>First Name</th>
<th>Last Name</th>
<th>Dept</th>
<th>Assyst Name</th>
</tr>
</thead>
<tbody>
<tr>
<td>#{ Html.BeginForm("AddEditUser", "Home"); }
<input type="submit" value="Add" id="btnSave" name="cmd" class="plain-button" />
</td>
<td>#Html.Editor("Forename")</td>
<td>#Html.Editor("Surname")</td>
<td>#Html.DropDownList("lstDepts")</td>
<td>#Html.Editor("AssystName")
#{ Html.EndForm(); }</td>
</tr>
#foreach (var User in Model)
{
<tr>
<td>#Html.ActionLink("Edit", "AddEditUser", new { id = User.ID }, new { name = "cmd", value = "Edit" })</td>
<td>
#User.Forename
</td>
<td>
#User.Surname
</td>
<td>
#User.Dept
</td>
<td>
#User.AssystName
</td>
</tr>
}
</tbody>
</table>
What i want to achieve is when a edit is pressed, the row that the edit link was on turns into edit boxes, i how to load up the add form with the edit users details (i could use hidden id and set it with querystring) but i wanted to edit in place really
anyone know how to do this or can point me in the right place?
THanks

ASP MVC passing model data in view back as form data

I got the following Razor view that display a list of HDD so an user and add to a cart. When the user press the button next to each row of HDD, it will pass the quantity and HDD's identity to a controller. However, while each HDD's ID does display properly, "hddId" is always 1 and "quantity" is correctly 5 when I inspect the Controller's parameters.
#model IEnumerable<TestStore.Models.Hdd>
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
#using(Html.BeginForm("AddToCart","Cart")){
<table>
<tr>
<th>
Ident
</th>
<th>
Brand
</th>
<th>
Name
</th>
<th>
Model
</th>
<th>
Speed
</th>
<th>
Capacity
</th>
<th>
Cache
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.hddId)
</td>
<td>
#Html.Hidden("hddId", item.hddId)
#Html.Hidden("quantity", 5)
#Html.DisplayFor(modelItem => item.brand)
</td>
<td>
#Html.DisplayFor(modelItem => item.name)
</td>
<td>
#Html.DisplayFor(modelItem => item.model)
</td>
<td>
#Html.DisplayFor(modelItem => item.speed)
</td>
<td>
#Html.DisplayFor(modelItem => item.capacity)
</td>
<td>
#Html.DisplayFor(modelItem => item.cache)
</td>
<td>
<input type="submit" value="Add to Cart" />
</td>
</tr>
}
</table>
}
Codingbiz is correct. The submit button wasn't referring to any particular row but the entire table. I suspect the reason why "hddId" was always "1" is because that's the first reference of "hddId" it see when the form is submitted. I changed it so #using(Html.BeginForm(...)) is inside the #foreach loop so each row has its own form and submit button:
#foreach (var item in Model) {
<tr>
#using(Html.BeginForm("AddToCart","Cart")){
<td>
#Html.DisplayFor(modelItem => item.hddId)
</td>
<td>
#Html.Hidden("hddId", item.hddId)
#Html.Hidden("quantity", 5)
#Html.DisplayFor(modelItem => item.brand)
</td>
<td>
#Html.DisplayFor(modelItem => item.name)
</td>
<td>
#Html.DisplayFor(modelItem => item.model)
</td>
<td>
#Html.DisplayFor(modelItem => item.speed)
</td>
<td>
#Html.DisplayFor(modelItem => item.capacity)
</td>
<td>
#Html.DisplayFor(modelItem => item.cache)
</td>
<td>
<input type="submit" value="Add to Cart" />
</td>
}
</tr>
}
As others have said, this doesn't work because when you submit, you're submitting every row in the table, not just the one clicked.
One option is to do what they suggest in this article:
http://federmanscripts.com/2010/01/12/form-and-table-row-nesting-workaround/
That is, using javascript, you copy the form values on button press to a hidden row and submit that.
I got around the validation problem by using button type="submit" instead of input type="submit". It still works correctly by passing the id number to the controller.
#using(Html.BeginForm("AddToCart","Cart")){
<table>
<tr>
//header stuff here
</tr>
#foreach (var item in Model) {
//row data goes here
//remove hidden field reference to #item.hddId
<tr>
<td>
<button type="submit" value="#item.hddId" name="hddId">Add to Cart</button>
</td>
</tr>
</table>
}

Categories

Resources