I have a table with some column in a asp.net mvc core project.
My view file looks like this
<h1>Items</h1>
<div class="panel-body">
<table class="table table-bordered table-responsive table-hover">
<thead>
<tr>
<th>Id</th>
<th>Title</th>
<th>Rating</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach (var item in Model.Items)
{
<tr onclick="location.href='#(Url.Action("ShowItemDetails", "Item", new {Id = item.FilmId}))'">
<td>#item.Id</td>
<td>#item.Title</td>
<td>#item.Rating</td>
<td>#Html.ActionLink("Edit", "Edit", "Show", new { id = item.Id }) | #Html.ActionLink("Rate this", "RateItem", new { id = item.Id }) </td>
</tr>
}
</tbody>
</table>
</div>
The problem is that when i click on a row, the controller method ShowItemDetails is called twice(!).
I can not see from the code above why this happens. Also, clicking on Edit or Rate this calls first ShowItemDetails and then immediately Edit or RateItem method in controller. Any suggestion how this can be solved?
Clicking on Edit or Rate this calls first ShowItemDetails and then immediately Edit or RateItem method because Edit is under a table row and on tablerow, you have called showitemdetails action.so, when you click on td , it gets first executed row action then td action.that's why it get called twice.
I hope, you want to show details and edit options with data of table and Edit is controller name.
Tweak your table code like below:
<tbody>
#foreach (var item in Model.Items)
{
<tr>
<td>#Html.ActionLink("Show Details","ShowItemDetails","Item",new {Id = item.FilmId})</td>
<td>#item.Id</td>
<td>#item.Title</td>
<td>#item.Rating</td>
<td>#Html.ActionLink("Edit", "Edit", "Show", new { id = item.Id }) | #Html.ActionLink("Rate this", "RateItem", new { id = item.Id }) </td>
</tr>
}
</tbody>
The problem seems to be caused be something i thought was irrelevant
having a null image cause the method to be called twice. Setting it to
solves this.
Need to add code to check for Model.Image != null
Very strange!!
Related
I am new in ASP.NET MVC. There are 2 tables in my database named "tbl_Project" and "tbl_Note". Each project can have one or more notes, so I keep/save the "ProjectID" variable in the "tbl_Note".
What I want to do: On the page where the project list is located, I want to show the total number of notes for each project. I tried a few things but I've fail.
This is my projects list page:
#if(Model.Any())
{
<table class="table table-striped table-hover table-bordered" id="sample_editable_1">
<thead>
<tr>
<th>Total Note</th>
<th>Project Name</th>
<th>Contract Start Date</th>
<th>Contract End Date</th>
</tr>
</thead>
#foreach (var item in Model)
{
<tbody>
<tr>
<td>
<!-- Total number of notes will come here -->
</td>
<td>
<p>#item.ProjectName</p>
</td>
<td>
#item.ContractStartDate.Value.ToString("dd.MM.yyyy");
</td>
<td>
#item.ContractEndDate.Value.ToString("dd.MM.yyyy");
</td>
</tr>
</tbody>
}
</table>
}
else
{
<p>Project is not available!</p>
}
I've try something like this but it's not working:
#if(item.tbl_Note != null)
{
if(item.tbl_Note.ProjectID == Model.ProjectID)
{
#Model.Sum(b => b.tbl_Note.ProjectID.Count)
}
}
This line gives an error: item.tbl_Note.ProjectID and the error is: 'ICollection' does not contain a definition for 'ProjectID and no extension method 'ProjectID' accepting a first argument of type 'ICollection' could be found.
How can I calculate the total number of notes? And if there is any other code block you want to insert, please tell me.
According to the error, item.tbl_Note is of type ICollection<T> and you are looking for a ProjectID property in it.
Change it to:
#if(item.tbl_Note != null)
{
#item.tbl_Note.Count
}
I have a DataTable in OrderDetails.aspx.vb
Public Outlets As New DataTable
Outlets = objOrdersData.GetOnlineConfigCall()
In my OrderDetails.aspx
<script>
var Outlets = '<%=Outlets%>';
</script>
I want to display outlets details in a html table using AngularJS
I defined my html table as
<table class="table">
<thead>
<tr> <th>Outlets</th>
</tr>
</thead>
<tbody>
<tr data-ng-repeat="outlet in Outlets">
<td><a >{{outlet.PartnerName}}</a></td>
</tr>
</tbody>
</table>
but it's not working.
Provided that Outlets is a valid js array, you just need to assign it to scope variable in your controller like:
OrderApp.controller("OrderCntrl", ['$scope', function ($scope) {
$scope.outlets = Outlets;
}]);
and then bind your data in the template like:
<tr ng-repeat="outlet in outlets">
<td><a >{{outlet.PartnerName}}</a></td>
</tr>
I couldn't find something that will help me to solve this issue in Angular2. I'd like to set a css class when I select a row. (without using jQuery)
<table class="table table-bordered table-condensed table-hover">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Website</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let item of companies" (click)="selectedCompany(item, $event)">
<td>{{item.name}}</td>
<td>{{item.email}}</td>
<td>{{item.website}}</td>
</tr>
</tbody>
</table>
I'm using Angular2 final release
There are plenty of solutions to do this, one of them is you can store the current company when clicked.
In the *ngFor you check if the current item is the currentCompany and you add the class highlighted or whatever class you wish if its the same company.
export class TableComponent {
public currentCompany;
public selectCompany(event: any, item: any) {
this.currentCompany = item.name;
}
}
And then on your template:
<tr *ngFor="let item of companies" (click)="selectCompany($event, item)"
[class.highlighted]="item.name === currentCompany">
--
Another solution if you wish to have multiple highlighted companies you can add a property highlighted to your item. Then on selectCompany() you just set the property to true. On your check you do [class.highlighted]="item.highlighted".
I know this was answered a while ago, but to expand on the accepted answer, you could also use [ngClass]="{'class_name': item.id === currentCompany }". The table hover may need to be removed as it may hide the background color change
<tr *ngFor="let item of companies" (click)="selectCompany($event, item)" [ngClass]="{'class_name': item.id === currentCompany }" >
Then css
.class_name{ background-color: yellow; }
So I'm working on an MVC 3 project that pulls from multiple (10) tables from a legacy data source to a Master View with 6 partials. There is one table that has data on every child view, so we decided to store that in session data and then populate the rest of the child views with whatever other data is needed.
When we originally tried to do this, we were getting null reference exceptions to the session data. I've come up with a solution, but it seems very clunky and I don't think it's best practices/introducing unnecessary state.
Relevant code to follow:
This is what we have on the main controller:
public ActionResult PolicyView(string PolicyID)
{
IPolicyHolder phdata = new PolicyHolderData();
Polmast policy = phdata.GetPolicyFromUV(PolicyID);
ViewBag.FullName = policy.FULLNAME;
ViewBag.PolicyID = PolicyID;
Session["polmast"] = policy;
return View("PolicyView");
}
And then in our main view, one of the links to the partial child views:
<div id="Billing">
#{ Html.RenderAction("Billing", Session["polmast"] ); }
</div>
In the child controller:
public ActionResult Billing(object sessiondata)
{
return PartialView("_Billing", sessiondata);
}
And in the child view:
#{var polmast = (Polmast)Session["polmast"];}
**snip**
<table id="premiumsgrid" class="display" border="1"
cellpadding="0" cellspacing="0" width="50%">
<thead>
<tr>
<th>Annual</th>
<th>Semi-Annual</th>
<th>Quarterly</th>
<th>Monthly</th>
</tr>
</thead>
<tbody>
<tr>
<td>#polmast.PAN</td>
<td>#polmast.PSA</td>
<td>#polmast.PQT</td>
<td>#polmast.PMO</td>
</tr>
</tbody>
</table>
I would recommend starting to use models and returning those to your views instead of passing around the session object and casting it within your view. It would make this code much more clean.
This is how I would structure my code:
public ActionResult PolicyView(string PolicyID)
{
IPolicyHolder phdata = new PolicyHolderData();
Polmast policy = phdata.GetPolicyFromUV(PolicyID);
PolicyModel model = new PoliceModel() {
FullName = policy.FULLNAME,
PolicyID = PolicyID
//Populate other properties here.
};
Session["polmast"] = policy;
return View("PolicyView", model);
}
Then I would set up your main view (there's no need to wrap this call in curly braces and you shouldn't need to pass any route values):
<div id="Billing">
#Html.RenderAction("Billing")
</div>
The child controller:
public ActionResult Billing()
{
//Get the data out of session; it should already exist since your parent controller took care of it.
var policyData = (Polmast)Session["polmast"];
PolicyModel model = new PoliceModel() {
FullName = policy.FULLNAME,
PolicyID = PolicyID
//Populate other properties here.
};
return PartialView("_Billing", model);
}
And your child view:
#model Polmast
snip
<table id="premiumsgrid" class="display" border="1"
cellpadding="0" cellspacing="0" width="50%">
<thead>
<tr>
<th>Annual</th>
<th>Semi-Annual</th>
<th>Quarterly</th>
<th>Monthly</th>
</tr>
</thead>
<tbody>
<tr>
<td>#Model.PAN</td>
<td>#Model.PSA</td>
<td>#Model.PQT</td>
<td>#Model.PMO</td>
</tr>
</tbody>
</table>
I am trying to render a table, using EditorFor, and a partialview, I think.
I have a model with a List<> property defined like this:
public List<TransactionSplitLine> TransactionSplitLines { get; set; }
The idea is that a user selects a few drop downs and enters a value into an edit box, and clicks a button. The model goes back to the controller, and the controller adds the entered values to the List<>
[HttpPost]
public ActionResult AccountTransaction(AccountTransactionView model)
{
var reply = CreateModel(model);
if (model.CategoryIds != null)
{
foreach (var c in model.CategoryIds)
{
reply.TransactionSplitLines.Add(new TransactionSplitLine { Amount = "100", Category = "Test Category", SubCategory = "Test More", CategoryId = int.Parse(c) });
}
}
reply.TransactionSplitLines.Add(new TransactionSplitLine { Amount = "100", Category = "Test Category", SubCategory = "Test More", CategoryId = 1 });
return View("AccountTransaction", reply);
}
Ignore the CreateModel. It simply sets up some data. Also, I am hardcoding data. This will eventually come from some form values.
The model is then returned to the same screen, allowing the user to ender more data. Any items in the List<> are read and a Table is rendered. I also have to store the current listen item values in hidden fields, so that they can be submitted back, along with the new data entered, so that the list can grow each time the user adds data.
The view is defined like this:
<table width="600">
<thead>
<tr class="headerRow">
<td>
Category
</td>
<td>
Sub Category
</td>
<td>
Amount
</td>
</tr>
</thead>
<tbody>
<%=Html.EditorFor(m=>m.TransactionSplitLines) %>
</tbody>
</table>
This is my first attempt with EditorFor...
My View is in a folder 'Views/BankAccount/AccountTransaction.aspx
I have created a ascx in Views/Shared/TransactionSplitLines.ascx
The code for the ascx is like this:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<BudgieMoneySite.Models.TransactionSplitLine>" %>
<tr>
<td>
<%=Model.Category %>
<%=Html.HiddenFor(x => x.CategoryId)%>
</td>
<td>
<%=Model.SubCategory %>
<%=Html.HiddenFor(x => x.SubCategoryId)%>
</td>
<td>
<%=Model.Amount %>
<%=Html.HiddenFor(x => x.AmountValue)%>
</td>
</tr>
This is data
The 'This is data' is just test stuff, which is never rendered.
When I run this, all that happens is that my output is rendered as:
<table width="600">
<thead>
<tr class="headerRow">
<td>
Category
</td>
<td>
Sub Category
</td>
<td>
Amount
</td>
</tr>
</thead>
<tbody>
Test Category
</tbody>
</table>
It seems like the ascx isn't being used? I'd expect to see the 'This is data' text. But, nothing. Hopefully you can see an obvious fault?
Your editor template should be either:
~/Views/Shared/EditorTemplates/TransactionSplitLine.ascx
or:
~/Views/BankAccount/EditorTemplates/TransactionSplitLine.ascx
The name of the ascx is always the type name of the collection item (TransactionSplitLine and not TransactionSplitLines) and it should be situated at ~/Views/Shared/EditorTemplates or ~Views/ControllerName/EditorTemplates.
Or if you want to use a custom editor template name:
<%= Html.EditorFor(m=>m.TransactionSplitLines, "~/Views/foo.ascx") %>
Or use UIHintAttribute on your model.