简体   繁体   中英

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

I define the section in Index. cshtml but when I run the application then generate an error section is not defined I create the same another project, then define the simple method in Index. cshtml then run my application but my current project popup not be displayed??

another project _layout.cshtml

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

Index.cshtml

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

that is work in another project

but my current working project my popup should not be displayed?? I m performing crud operation using api

_layout.cshtml

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

Index.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>
}

HomeController.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");
            }
        }

My record should be created, but I want to popup message when submits the record?? but give the error section is not defined?? What I am trying I went to when I press the submit button, then a popup should be displayed but popup not display?

I hope my question is understood?

I m going to the browser and then ctrl + U then show the popup function.

<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>

To avoid section is not defined error you need to call RenderSection as below.

  @RenderSection("alertpopup", false)

And to make the pop up work, you have defined your javascript function but you are not calling you function anywhere. you can do this instead.

<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)

Rendering sections take two parameters; a string and a boolean as such:

@RenderSection(string name, bool required)

The name parameter is name of the section to render and required indicates that the section must always be rendered. If the section may not be rendered in some views or sometime, you should set required to false like below.

@RenderSection("alertpopup", false)

I'm also not sure when you want your popup to display because it is not called in your code. If for whatever reason you'll want it to be called when your documents loads, you should change your code to this

@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>
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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