[英]Problems with an if statement in my MVC3 razor view
I need to modify the display of elements in my razor view depending on the value of a variable that's in my model.我需要根据 model 中的变量值修改 razor 视图中元素的显示。
What I have is Model.PartitionKey which is a six character string.我拥有的是 Model.PartitionKey,它是一个六字符的字符串。 I need to code an if statement that will check if the 3rd and 4th characters of this string are "00".
我需要编写一个 if 语句来检查该字符串的第 3 个和第 4 个字符是否为“00”。
Here's what I have so far:这是我到目前为止所拥有的:
@using (Ajax.BeginForm(
action,
"Contents",
null,
new AjaxOptions
{
UpdateTargetId = "update-message",
InsertionMode = InsertionMode.Replace,
HttpMethod = "POST",
OnSuccess = success,
OnFailure = "ajaxOnFailure"
}, new { @id = "dialogForm", @class = "content ui-widget dialog-admin" }))
{
<div class="float-left">
@Html.LabelFor(model => model.Status)
@if (action == "Edit" || action == "Create") {
@Html.DropDownList("Status", null, new { @class="drop-down", id = "dialogStatus", style="width: 120px" })
}
else
{
@Html.TextBox("Status", (string)ViewBag.Status, new { @readonly = true })
}
</div>
@if ( Model.PartitionKey.Substring(2,2) == "00") {
<div class="float-left">
Html.LabelFor(model => model.Link)
if (action == "Edit" || action == "Create") {
Html.TextBoxFor(model => model.Link, new { size = 25 })
} else {
Html.TextBoxFor(model => model.Link, new { size = 25, @readonly = true })
}
</div>
}
The problem is that it there is an error message on line starting with @if on the 3rd character "f" saying "unexepected if keyword after the @ character".问题是它在第 3 个字符“f”上以 @if 开头的行上有一条错误消息,说“unexepected if keyword after the @ character”。
Note that I have @ characters before the if in the first block of code.请注意,我在第一段代码中的 if 之前有 @ 字符。 However it is just in the second block of code that it complains.
然而,它只是在它抱怨的第二个代码块中。
Try尝试
@using (Ajax.BeginForm(
action,
"Contents",
null,
new AjaxOptions
{
UpdateTargetId = "update-message",
InsertionMode = InsertionMode.Replace,
HttpMethod = "POST",
OnSuccess = success,
OnFailure = "ajaxOnFailure"
}, new { @id = "dialogForm", @class = "content ui-widget dialog-admin" }))
{
<div class="float-left">
@Html.LabelFor(model => model.Status)
@if (action == "Edit" || action == "Create") {
@Html.DropDownList("Status", null, new { @class="drop-down", id = "dialogStatus", style="width: 120px" })
}
else
{
@Html.TextBox("Status", (string)ViewBag.Status, new { @readonly = true })
}
</div>
if ( Model.PartitionKey.Substring(2,2) == "00") {
<div class="float-left">
@Html.LabelFor(model => model.Link)
@if (action == "Edit" || action == "Create") {
@Html.TextBoxFor(model => model.Link, new { size = 25 })
} else {
@Html.TextBoxFor(model => model.Link, new { size = 25, @readonly = true })
}
</div>
}
}
I think you can't have an if inside an if in that sort.我认为你不能在那种 if 里面有 if 。 Try opening a code block with
尝试打开一个代码块
@{ if (boolean)
{
// some code
if (boolean)
{
//some code
}
}
}
Try if it works.试试看是否有效。
use something like this使用这样的东西
@{
if (Boolean)
{
// execute code here
}
else
{
// execute else code here
}
}
As per the following links:根据以下链接:
I know its been mentioned in comments above, but it maybe helpful.我知道上面的评论中提到了它,但它可能有帮助。 So based on that attached, does the following code work:
因此,基于所附的内容,以下代码是否有效:
@using (Ajax.BeginForm(
action,
"Contents",
null,
new AjaxOptions
{
UpdateTargetId = "update-message",
InsertionMode = InsertionMode.Replace,
HttpMethod = "POST",
OnSuccess = success,
OnFailure = "ajaxOnFailure"
}, new { @id = "dialogForm", @class = "content ui-widget dialog-admin" }))
{
<div class="float-left">
@Html.LabelFor(model => model.Status)
@if (action == "Edit" || action == "Create") {
@Html.DropDownList("Status", null, new { @class="drop-down", id = "dialogStatus", style="width: 120px" })
}
else
{
@Html.TextBox("Status", (string)ViewBag.Status, new { @readonly = true })
}
</div>
@if ( Model.PartitionKey.Substring(2,2) == "00") {
<div class="float-left">
Html.LabelFor(model => model.Link)
if (action == "Edit" || action == "Create") {
@Html.TextBoxFor(model => model.Link, new { size = 25 })
} else {
@Html.TextBoxFor(model => model.Link, new { size = 25, @readonly = true })
}
</div>
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.