在MVC剃须刀中实现if else [英] Implementation of if else in MVC razor
问题描述
我是ASP.Net MVC的新手。对不起,如果我的问题似乎很愚蠢。但我不确定如何在视图和控制器之间进行操作。
我有一个价格的产品清单,并有静态下拉,以根据所选的折扣计划实施折扣策略。
这是我的产品页面
I am pretty new to ASP.Net MVC. Sorry if my question seems stupid. but I am not sure how to get things to work between views and controllers.
I have a product list with the price and have static drop down to implement discount strategies based on discount plan selected.
Here is my product page
<pre><table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.ProductName)
</th>
<th>
@Html.DisplayNameFor(model => model.ProductPrice)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.ProductName)
</td>
<td>
@Html.DisplayFor(modelItem => item.ProductPrice)
</td>
<td>
@Html.DropDownList("Status", new List<SelectListItem>
{
new SelectListItem{ Text="Student Discount", Value = "1" },
new SelectListItem{ Text="Loyalty Discount", Value = "0" }
})
</td>
<
这里是m y添加产品控制器
here is my Add product Controller
public ActionResult Index()
{
Customers customer = new Customers();
return View(db.Products.ToList());
}
我尝试过:
我想根据下拉菜单中的选项显示页面上的折扣价。
我正在实现这个Windows窗体应用程序我的MVC应用程序中的stretegy。
但我不知道如何在视图和控制器中处理它。
任何提示都将不胜感激。
这里是来自windows表单应用程序的代码
What I have tried:
I want to show discounted price on the page based on the selection from the dropdown.
I am implementing this windows form application stretegy in my MVC application .
But i am not sure how to handle it in views and controller.
Any hints would be appreciated.
here is the code from windows form applicaion
private void btnApplyDiscounts_Click(object sender, EventArgs e)
{
Customer customer = new Customer();
if (chkLoyalty.Checked && chkStudent.Checked)
{
customer.SetDiscountStrategy(new LoyalStudentDiscount());
}
else if (chkLoyalty.Checked)
{
customer.SetDiscountStrategy(new LoyaltyDiscount());
}
else if (chkStudent.Checked)
{
customer.SetDiscountStrategy(new StudentDiscount());
}
else
{
customer.SetDiscountStrategy(new NoDiscount());
}
decimal price = customer.ApplyDiscount(numTotalSale.Value);
lblFinalPrice.Text = price.ToString("C");
}
}
}
推荐答案
Razor是服务器端的html渲染引擎。因此,在将页面发送到用户的Web浏览器之前。
如果您想在客户端进行更改,在浏览器中加载页面后,您将需要使用javascript / jquery / etc ...
Razor is a server-side html rendering engine. So before the page is sent to the user's web browser.
If you want to make changes client-side, after the page is loaded in the browser, then you will need to use javascript/jquery/etc...
这篇关于在MVC剃须刀中实现if else的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!