加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 获取错误 – “@”字符后面的意外“if”关键字.

发布时间:2020-12-16 09:12:52 所属栏目:asp.Net 来源:网络整理
导读:参见英文答案 Unexpected “foreach” keyword after “@” character????????????????????????????????????2个 我在视图中得到错误 – “”test1.cshtml“ “@”字符后面的意外“if”关键字.一旦进入代码,你就不需要像“if”这样的结构加前缀 test1.cshtml
参见英文答案 > Unexpected “foreach” keyword after “@” character????????????????????????????????????2个
我在视图中得到错误 – “”test1.cshtml“

“@”字符后面的意外“if”关键字.一旦进入代码,你就不需要像“if”这样的结构加前缀

test1.cshtml

@model WebApplication9.Models.Names


@using (Html.BeginForm())
{
    @Html.TextBoxFor(m => m.MyName)

    <button type="submit">Submit</button>

    if (!string.IsNullOrWhiteSpace(Model.MyName))
    {
        <p>welcome,your name is @Model.MyName</p>
    }
}

控制器代码

public ActionResult test1()
        {

            Names name = new Names();

            return View(name);

            ViewBag.Message = "Your contact page.";

            return View();
        }
        [HttpPost]
        public ActionResult test1(string name)
        {
            ViewBag.Message = "Hello what is ur name ???";
            ViewBag.Name = name;
            return View();
        }

型号代码

namespace WebApplication9.Models
{
    public class Names
    {
        public string MyName { get; set; }
    }
}

解决方法

尝试:

@model WebApplication9.Models.Names


@using (Html.BeginForm())
{
    @Html.TextBoxFor(m => m.Name)

    <button type="submit">Submit</button>

    if (!string.IsNullOrWhiteSpace(Model.MyName))
    {
        <p>welcome,your name is @Model.MyName</p>
    }
}

由于你有@using,里面的Razor代码不需要@.但是,如果你有一个HTML元素,那么你需要使用@,就像欢迎文本一样.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读