我目前正在开展一个小型电子商务项目.我想显示产品的小描述,而不是显示所有描述(从数据库中提交).
这是我的控制器;
var products = context.Products.OrderBy(p => p.ProductName);
@ViewBag.ProductList = products.ToList<Product>();
Run Code Online (Sandbox Code Playgroud)
这是我的观看代码;
<ul class="display" id="content">
@foreach( var item in @ViewBag.ProductList as IEnumerable<Product>)
{
@Html.Partial("_ProductPartial", item)
}
</ul>
Run Code Online (Sandbox Code Playgroud)
现在在我的Partial "_ProductPartial"视图中,我有一个名为的描述字段;
<p>
@Html.DisplayFor(model => model.ProductDescription)
</p>
Run Code Online (Sandbox Code Playgroud)
现在,我想显示产品的简短描述(而不是显示默认情况下的整个描述).
那么,我如何使用LINQ做到这一点?
有没有其他办法(如果可能的话)?
昨晚我觉得asp.net mvc中有不同的视图引擎;
Spark
Web Forms
Razor
Run Code Online (Sandbox Code Playgroud)
(我知道语法也不同但是)
它们会影响asp.net mvc项目的性能吗?
假设我想使用"Razor"而不是Web Froms.那么,我会受到性能影响吗?如是.请!说明.提前致谢!
可能重复:
foreach循环如何在C#中工作?
就像经典迭代语句一样,for,while或do-while,is foreach loop is a new loop statment in c#?或in other languages such as php
要么
在幕后,它将我们的代码转换为for,while或do-while循环.
我有一个存储过程用于我的购物车系统,它返回总金额,看起来像这样;
ALTER PROCEDURE [dbo].[ShoppingCartGetTotalAmount]
(@CartID char(36))
AS
SELECT ISNULL(SUM(Product.Price * ShoppingCart.Quantity), 0)
FROM ShoppingCart INNER JOIN Product
ON ShoppingCart.ProductID = Product.ProductID
WHERE ShoppingCart.CartID = @CartID
Run Code Online (Sandbox Code Playgroud)
但是,现在我想在Entity Framework中做同样的事情.因此,我需要知道以下任一选项;
1)如何在Entity FrameWork中执行上述任务,即
SELECT ISNULL(SUM(Product.Price * ShoppingCart.Quantity), 0)
FROM ShoppingCart INNER JOIN Product
ON ShoppingCart.ProductID = Product.ProductID
WHERE ShoppingCart.CartID = @CartID
Run Code Online (Sandbox Code Playgroud)
2)或者C#中SQL ISNULL()函数的等效是什么?
3)或者我怎样才能实现这一点 - > ISNULL(SUM(Product.Price * ShoppingCart.Quantity), 0)
使用任何.Net方法?
我已经创建了一个wcf服务,但我已经使用了3个项目;
1)ServiceLibrary(WCF库)
2)Web
3)ConsoleTestClient
我的ServiceLibraryapp.config文件如下所示;
<system.serviceModel>
<services>
<service name="MrDAStoreJobs.ServiceLibrary.AdvertisementService">
<clear />
<endpoint address="basic"
binding="basicHttpBinding" bindingConfiguration=""
contract="MrDAStoreJobs.ServiceLibrary.Interface.IAdvertisementService" />
<endpoint name="mexHttpBinding"
contract="IMetadataExchange"
binding="mexHttpBinding"
address="mex" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:13758/" />
</baseAddresses>
</host>
</service>
</services>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information,
set the value below to false before deployment -->
<serviceMetadata httpGetEnabled="True" />
<!-- To receive exception details in faults for debugging purposes,
set the value below to true. Set to false before deployment
to avoid …Run Code Online (Sandbox Code Playgroud) 我试图循环多级动态菜单.我已成功手动执行此操作,即每次如果要显示Menus其父项的子项,我必须手动循环.我想知道通过这些动态菜单循环多级的最佳方式或替代方法
这是我到目前为止所做的事情;
@{ var menusList = ViewBag.Menus as IEnumerable<ParentMenuViewModel>; }
@foreach (var parentMenu in menusList.Where(p => p.ParentId == 0))
{
<ul>
<li>
<h1>@parentMenu.Name</h1>
@if (menusList.Count(p => p.ParentId == parentMenu.MenuId) > 0)
{
<ul>
@foreach (var childMenu in menusList.Where(p => p.ParentId == parentMenu.MenuId))
{
<h2>@childMenu.Name</h2>
if (menusList.Count(p => p.ParentId == childMenu.MenuId) > 0)
{
foreach (var subChild in menusList.Where(p => p.ParentId == childMenu.MenuId))
{
<h3>@subChild.Name</h3>
}
}
}
</ul>
}
</li>
</ul>
}
Run Code Online (Sandbox Code Playgroud)
更新:输出看起来像这样;
HOME
SUB MENU1 …Run Code Online (Sandbox Code Playgroud) 是否可以在列表视图中执行以下操作?如果不。请纠正我。
<asp:ListView runat="server" ID="lsit">
<LayoutTemplate>
<div id="banner">
<div id="paginate-slider2" class="banner_nav">
<asp:PlaceHolder runat="server" ID="itemPlaceHolder1" />
</div>
<div id="slider2">
<asp:PlaceHolder runat="server" ID="itemPlaceHolder2" />
</div>
<script src="/scripts/banner.js" type="text/javascript"></script>
</div>
<div class="clear"></div>
</LayoutTemplate>
<ItemTemplate>
<a href="#" class="toc">
<img src="./images/gallery/thumbnails/thumb1.gif" alt="" />
</a>
</ItemTemplate>
<ItemTemplate>
<div class="contentdiv banner_sec">
<div class="con_img">
<img src="./images/gallery/images/img1.gif" alt="" />
</div>
<div class="con_desc">
<h3>Featured</h3>
<h5>Lorem ipsum dolor sit amet</h5>
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Sed elit.
</p>
<br />
<a href="./detail.html" class="buttontwo"><span>Read More</span></a>
</div>
</div>
</ItemTemplate>
</asp:ListView> …Run Code Online (Sandbox Code Playgroud) 我用以下查询查询我的表;
SELECT DISTINCT [RoleId], [RoleName], [RoleDescription], [PermissionId]
FROM [Role]
ORDER BY [RoleName]
Run Code Online (Sandbox Code Playgroud)
我得到以下结果;
RoleId RoleName RoleDescription PermissionId
1 Admin CanEdit 1
2 Admin asdf;lkj 2
3 Admin al;dskfj;l 3
4 Admin fa;ldkfjas;d 4
17 SuperAdmin aslkdfja; 1
18 SuperAdmin asldkfa;f 2
15 Users alskdfj;alk 1
16 Users aslkdfja;sl 2
Run Code Online (Sandbox Code Playgroud)
但是,我想获取RoleName的唯一值,即不应重复RoleName,这已经在我的查询结果中.我想获得一次RoleName. 如何获得唯一的RoleName?
更新: 我想用以下结果填充组合框;
Admin
SuperAdmin
Users
Run Code Online (Sandbox Code Playgroud)
但不是不重复(我已经使用了我的查询
Admin
Admin
Admin
Admin
SuperAdmin
SuperAdmin
Users
Users
Run Code Online (Sandbox Code Playgroud) 我试图通过划分获得的标记*100 /总标记在第三个文本框中显示值; 但我无法做到这一点;
$(document).ready(function () {
$("#TextBoxTotalMarks, #TxBx_MarksObtained").keyup(function () {
var a = parseInt($("#TextBoxTotalMarks").val(), 10);
var b = parseInt($("#TxBx_MarksObtained").val(), 10);
var c;
if (a < b) {
c = "sorry not accepted";
}
else {
c = ((b * 100) / a).toFixed(2);
}
$("#TextBoxMarksInPercent").val(c);
});
});
Run Code Online (Sandbox Code Playgroud)
当我在总标记文本框中输入类似100的内容而在标记获得的文本框中输入300时,我看不到消息"抱歉未被接受"
<tr>
<td class="shade">
<asp:Literal ID="Ltrl_TotalMarks" runat="server" Text="Total Marks"></asp:Literal>
</td>
<td>
<asp:TextBox ID="TextBoxTotalMarks" runat="server" />
</td>
</tr>
<tr>
<td class="shade">
<asp:Literal ID="Ltrl_MarksObtained" runat="server" Text="Marks Obtained"></asp:Literal>
</td>
<td>
<asp:TextBox ID="TxBx_MarksObtained" runat="server"
</td>
</tr>
<tr>
<td …Run Code Online (Sandbox Code Playgroud) 我只是急于知道,为什么身份===运算符在c#中不可用?有什么理由不可用.c#中===运算符的等价值是多少? 因为我在一个地方看到身份运算符===比等于==运算符更快.因为==运算符暂时更改数据类型.
更新:===与==运营商之间的差异.===基本上不会将两个变量转换为临时数据类型,而==等于临时转换转换变量的数据类型