MVC checkbox event in grid - asp.net-mvc

I am a new bee in MVC world. I have a scenario like, i have an grid with a checkboxs as one column. When i click on checkbox an event would fire and it would update some values in database.
I am using Razor engine.
<table>
<tr>
<th>
ID
</th>
<th>
PName
</th>
<th>
PDescription
</th>
<th>
PSerialNo
</th>
<th>
PPrice
</th>
<th>
PActive
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#item.ID
</td>
<td>
#Html.ActionLink(#item.PName, "Edit", new { id = #item.ID })
</td>
<td>
#item.PDescription
</td>
<td>
#item.PSerialNo
</td>
<td>
#String.Format("{0:c}", item.PPrice)
</td>
<td>
#Html.CheckBox("chkActiveItem", item.PActive)
</td>
</tr>
}
here the content is showed in the grid. Here when i click on this check box i want to update the flag in the database.
How will i do it?
Please help.

foreach doesn't work in this case. You need to use for loop and then changes will get picked up on server side on post.
if you want to do ajax call then simply store id of each item within its row and pass that as parameter during ajax call to identify checked item on server side.

Related

Table Column NO for Local Variable in MVC View Page

Possible for my question?
My Application in Product Table.
No Name
1 Apple
2 Orange
3 PipleApple etc....
This above table of No is Sorted By Product ID.But my user is wanted some time prodect table in above product item deleted.Then ID is not ascending.Therefore Number ascending for want local variable in MVC Table of Index.chtml.Possible ....?
#model IEnumerable<RenewableEnergyProjcet.Models.REHPDataModels.ProductTable>
<table >
<thead>
<th>
#Html.Label("No")
</th>
<th>
#Html.Label("Name")
</th>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
//this part for dynamic local variable assign
</td>
<td >
#Html.DisplayFor(modelItem => item.Name)
</td>
}
</tbody>
</table>

Redirect to Link from Link saved in database

I am developing an Web Application in MVC.
I have added a link in Database eg:"www.google.com", I want user to redirect to Google on click.
I tried in many ways but getting the controller in address bar "eg:http://localhost:/Home/www.google.com"
View Code
<table class="table" style="margin-top:10px;">
<tr>
<th>
Sr. No.
</th>
<th>
Name
</th>
<th>
Details
</th>
<th>
Status
</th>
<th>
Tracking No.
</th>
<th>
Tack Order
</th>
<th>
Ordered On
</th>
</tr>
#{int row = 0;} #foreach (var item in Model) { if (item.PaymentSuccessfull == true) {
<tr>
<td>
#(row += 1)
</td>
<td>
#Html.DisplayFor(modelItem => item.DeliveryDetail.FirstName)
</td>
<td>
Order Details
</td>
<td>
#Html.DisplayFor(modelItem => item.Status)
</td>
<td>
#Html.DisplayFor(modelItem => item.TrackingId)
</td>
<td>
//Here I want user to redirect to Google on click
Track
</td>
<td>
#Html.DisplayFor(modelItem => item.DateTime)
</td>
</tr>
} }
Help is appreciated.
Thanks in advance.
You must store the protocol with the rest of the URL.
so:
www.google.com
becomes:
http://www.google.com

MVC Entity Framework INDEX page format

I have a new MVC Entity Framework Application. When it created the Index List page - it looks like the Headers are ran together. Have small size fields and the field names are longer than the fields. How to format that page to make Header spaced out so it doesn't all run together.
Could not post a pic - something like this - sorry the data line ran together on the past.
RE NUMINT RE NAME
1 I SHAWN NEWT
#model IEnumerable<MTSapp.Models.mts_rename>
#{ ViewBag.Title = "Index"; }
<h2>Index</h2>
<p> #Html.ActionLink("Create New", "Create") </p>
<table>
<tr>
<th> #Html.DisplayNameFor(model => model.A_RENO) </th>
<th> #Html.DisplayNameFor(model => model.A_INACT) </th>
<th> #Html.DisplayNameFor(model => model.A_NAME) </th>
<th></th>
</tr>
#foreach (var item in Model)
{
<tr>
<td> #Html.DisplayFor(modelItem => item.A_RENO) </td>
<td> #Html.DisplayFor(modelItem => item.A_INACT) </td>
<td> #Html.DisplayFor(modelItem => item.A_NAME) </td>
</tr>
}
</table>
Is the page using a table or some type of grid? You may need to apply some CSS rules to get the page nicely formatted. Does the page have any CSS styling yet?

View renders but doesn't update

Ive got a view that i pass a model to. I want it to render out a table out of a list in the model. It does that just fine it renders the view in that case that if i debugg it i can se the values goes to the right places. But the browser never updates. If i press ctrl+R in the browser it renders the view again in the exact same way but this time it show the list in the browser. Can i make this update everytime the view renders so that i don't have to press ctrl+R?
[HttpPost]
public ActionResult CreateResource(ViewModel view)
{
view.ResourceDateCreated = DateTime.Now;
viewmodel = view;
Session["ViewModel"]=viewmodel;
return View("Index", viewmodel);
}
The view:
<table class="table">
<tr>
<th>
MetaDataName
</th>
<th>
MetaDataDescription
</th>
<th>
LanguageCode
</th>
<th>
UserId
</th>
<th>
MetaDataDateCreated
</th>
#*<th>#Html.DisplayNameFor(model => model.MetaList)</th>*#
</tr>
#if (Model.Metadata.Count != 0)
{
foreach (var item in Model.Metadata)
{
<tr>
<td>
#item.MetaDataName
</td>
<td>
#item.MetaDataDescription
</td>
<td>
#item.LanguageCode
</td>
<td>
#item.UserId
</td>
<td>
#item.MetaDataDateCreated.ToString()
</td>
<td>
<table>
<tr>
<th>
MetaListId
</th>
</tr>
#if (item.MetaList.Count != 0)
{
foreach (var list in item.MetaList)
{
<tr>
<td>
#list.MetaListId
</td>
</tr>
}
}
</table>
</td>
</tr>
}
}
</table>

Is there a way to make the MVC Html.Actionlink invisible based on a property value

Hi say I have a view containing:
<table>
<tr>
<th>
Invoice Number
</th>
<th>
Invoice Date
</th>
<th>
Organisation
</th>
<th>
Region
</th>
<th>
Total Excluding GST
</th>
<th>
Total Including GST
</th>
<th>
</th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.InvoiceNumber)
</td>
<td>
#Html.DisplayFor(modelItem => item.InvoiceDate, "{0:D}")
</td>
<td>
#Html.DisplayFor(modelItem => item.Organisation.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.Area.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.TotalExcludingGst)
</td>
<td>
#Html.DisplayFor(modelItem => item.TotalIncludingGst)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id = item.Id })
</td>
</tr>
}
</table>
and each item has a boolean property Editable. Is there a way to make the Edit link visible/invisible based on the value of this property?
You can use an if:
#if (item.Editable) {
#Html.ActionLink(...)
}
Basically you can use css. But if you insist in actionlink
Html.ActionLink(
"LinkName",
"Action",
null,
new { #style = "display:none" });
So if you use it with #if statement in view, you can achieve what you want.
Use CSS
I am invisible

Resources