在MVC视图页面中隐藏表单
本文关键字:隐藏 表单 MVC 视图 | 更新日期: 2023-09-27 18:11:48
我在MVC3视图页面中有2个表单,基于某些条件我必须隐藏一个表单,但我收到一些错误,无法找出错误是什么?下面是代码
@model Models.Common.Details
@{
if (ViewContext.IsChildAction != true && !Request.IsAjaxRequest())
{ Layout = "../../Shared/DefaultLayout.cshtml"; }
else
{ Layout = null; }
}
@using Models.Common;
@{ ViewBag.Title = "Premium";}
<script type="text/javascript" language="javascript">
var urlContent = "@Url.Content("~/")";
var currentYear = "@DateTime.Now.Year";
var currentMonth = "@DateTime.Now.Month";
</script>
<script src="@Url.Content("~/Scripts/UIScripts/paymentmodule/Premium.js?" + DateTime.Now)" type="text/javascript"></script>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
@Html.AntiForgeryToken()
@Html.HiddenFor(m => m.newPolicyDetails.ReferenceNumber1)
@Html.HiddenFor(m => m.newPolicyDetails.ReferenceNumber2)
@Html.HiddenFor(m => m.newPolicyDetails.ReferenceNumber3)
<input type="submit" id="Settle" name="Settle" value="Settle"/>
}
@{
if (!(Session["User"].Equals(Constant.User)))
{
@using (Html.BeginForm(null,null,FormMethod.post,new{@action="www.somelink.com/abce"}))
{
@Html.ValidationSummary(true)
@Html.AntiForgeryToken()
@Html.HiddenFor(m => m.newPolicyDetails.ReferenceNumber4)
@Html.HiddenFor(m => m.newPolicyDetails.ReferenceNumber5)
@Html.HiddenFor(m => m.newPolicyDetails.ReferenceNumber6)
<input type="submit" id="ok" name="Ok" value="Settle"/>
}
}
}
我已经使用此代码@{-----}来编写基于该条件的c#条件,我需要隐藏第二种形式,因为它是视图页面,我不确定什么错误即将到来,但我不能去这个视图页面本身。如果我删除代码块@{-----},一切工作正常。有什么建议吗?
Error Message:
UserMessage= Error Occurred at : 07/08/2014 12:03:02.547
Exception Details : System.Web.HttpParseException (0x80004005): Unexpected "using" keyword after "@" character. Once inside code, you do not need to prefix constructs like "using" with "@".
下面是if条件的正确语法
@if (!(Session["User"].Equals(Constant.User)))
{
using (Html.BeginForm(null, null, FormMethod.post, new { @action = "www.somelink.com/abce" }))
{
@Html.ValidationSummary(true)
@Html.AntiForgeryToken()
@Html.HiddenFor(m => m.newPolicyDetails.ReferenceNumber4)
@Html.HiddenFor(m => m.newPolicyDetails.ReferenceNumber5)
@Html.HiddenFor(m => m.newPolicyDetails.ReferenceNumber6)
<input type="submit" id="ok" name="Ok" value="Settle" />
}
}
尝试在使用begin form之前删除@
符号。并将FormMethod.post
改为FormMethod.Post
,大写P。
当你已经在c#代码中,你不需要在using之前使用@
像这样改变你的if,它是FormMethod.Post
而不是FormMethod.post
:
@if (!(Session["User"].Equals(Constant.User)))
{
using (Html.BeginForm(null,null,FormMethod.Post,new{@action="www.somelink.com/abce"}))
{
@Html.ValidationSummary(true)
@Html.AntiForgeryToken()
@Html.HiddenFor(m => m.newPolicyDetails.ReferenceNumber4)
@Html.HiddenFor(m => m.newPolicyDetails.ReferenceNumber5)
@Html.HiddenFor(m => m.newPolicyDetails.ReferenceNumber6)
<input type="submit" id="ok" name="Ok" value="Settle"/>
}
}
通过使用@,你告诉编译器c#代码开始了,所以在if块中,你不需要在 beginfo
如果喜欢
@if (!(Session["User"].Equals(Constant.User)))
{
//remove @ here and make p of post capital like Post
using (Html.BeginForm(null,null,FormMethod.Post,new{@action="www.somelink.com/abce"}))
{
@Html.ValidationSummary(true)
@Html.AntiForgeryToken()
@Html.HiddenFor(m => m.newPolicyDetails.ReferenceNumber4)
@Html.HiddenFor(m => m.newPolicyDetails.ReferenceNumber5)
@Html.HiddenFor(m => m.newPolicyDetails.ReferenceNumber6)
<input type="submit" id="ok" name="Ok" value="Settle"/>
}
}
虽然为了更好的可读性和维护,我建议你使用像
这样的部分页面@if (SomeCondition)
{
@Html.Partial("_firstPage")
}
else if (OtherCondition)
{
@Html.Partial("_secondPage")
}