繁体   English   中英

在asp.net mvc 的Index.cshtml 中未定义部分?

[英]Section not defined In Index.cshtml in asp.net mvc?

我在索引中定义了该部分。 cshtml 但是当我运行应用程序然后生成一个错误部分未定义我创建相同的另一个项目,然后在 Index.cshtml 中定义简单的方法。 cshtml 然后运行我的应用程序但我当前的项目弹出窗口没有显示?

另一个项目_layout.cshtml

<div class="container body-content">
        @RenderBody()
        @RenderSection("simpelmessage")
        <footer>
            <p>WebApi Crud Oeperation using Entity Framework In Mvc</p>
        </footer>
    </div>

索引.cshtml

@section simpelmessage{
   <h1>simple message</h1>
}

那是在另一个项目中的工作

但是我当前的工作项目不应该显示我的弹出窗口?? 我正在使用 api 执行 crud 操作

_layout.cshtml

    <div class="container body-content">
        @RenderBody()
        @RenderSection("alertpopup")
        <footer>
            <p>WebApi Crud Oeperation using Entity Framework In Mvc</p>
        </footer>
    </div>

索引.cshtml

@section alertpopup
{
    <script src="https://cdnjs.cloudflare.com/ajax/libs/AlertifyJS/1.13.1/alertify.min.js" type="text/javascript">
        $(function () {
            var successmessage = '@ViewBag.message'  
            if (successmessage != '') 
            {
                alertify.success(successmessage);
            }
        });
    </script>
}

家庭控制器.cs

        public ActionResult Index()
        {
            IEnumerable<studentmvcmodel> stdlist;
            HttpResponseMessage response = globalvariable.webapiclient.GetAsync("studlogins").Result;
            stdlist = response.Content.ReadAsAsync<IEnumerable<studentmvcmodel>>().Result;
            ViewBag.message = "record is inserted";
            return View(stdlist);
        }

        public ActionResult create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult create(studentmvcmodel stud)
        {
            var username = stud.username;
            var passs = stud.password;
            if (username != null && passs != null)
            {
                HttpResponseMessage response = globalvariable.webapiclient.PostAsJsonAsync("studlogins", stud).Result;
                ViewBag.message = "data inserted successfully";
                return RedirectToAction("Index");
            }
            else
            {
                ViewBag.message = "please all the data fillup carefully";
                return View("create");
            }
        }

我的记录应该被创建,但我想在提交记录时弹出消息?? 但给出错误部分未定义? 当我按下提交按钮时,我正在尝试什么,然后应该显示一个弹出窗口但不显示弹出窗口?

我希望我的问题被理解了?

我要去浏览器,然后ctrl + U然后显示弹出功能。

<script src="https://cdnjs.cloudflare.com/ajax/libs/AlertifyJS/1.13.1/alertify.min.js" type="text/javascript">        


        $(function popup()
        {
            var successmessage = 'record is inserted';
            if (successmessage != '')
            {
                //alertify.success(successmessage);
                alert('record is inserted');
            }
        });
        popup();
      </script>

为避免部分未定义错误,您需要按如下方式调用 RenderSection。

  @RenderSection("alertpopup", false)

为了使弹出窗口工作,您已经定义了您的 javascript 函数,但您没有在任何地方调用您的函数。 你可以这样做。

<script src="https://cdnjs.cloudflare.com/ajax/libs/AlertifyJS/1.13.1/alertify.min.js"  type="text/javascript"></script>
  <script>
      function popup () {
        var successmessage = '@ViewBag.message'  
        if (successmessage != '') 
        {
            alertify.success(successmessage);
        }
    }
  popup();
  </script>

如果你返回 View("create"),那么你应该在Create.cshtml渲染你的部分或者在你的布局中定义这个 @RenderSection("alertpopup", false)

渲染部分采用两个参数; 一个字符串和一个布尔值,例如:

@RenderSection(string name, bool required)

name参数是要呈现的部分的名称, required表示必须始终呈现该部分。 如果该部分可能无法在某些视图中或某些时候呈现,则应将 required 设置为 false,如下所示。

@RenderSection("alertpopup", false)

我也不确定您希望何时显示弹出窗口,因为它没有在您的代码中调用。 如果出于某种原因您希望在加载文档时调用它,则应将代码更改为此

@section alertpopup
{
    <script src="https://cdnjs.cloudflare.com/ajax/libs/AlertifyJS/1.13.1/alertify.min.js" type="text/javascript">
        $(doucument).ready(function () {
            var successmessage = '@ViewBag.message'  
            if (successmessage != '') 
            {
                alertify.success(successmessage);
            }
        });
    </script>
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM