How to show Alert Message like “successfully Inserted” after inserting to DB using ASp.net MVC3

前端 未结 4 1457
春和景丽
春和景丽 2020-12-30 02:41

How to write a code for displaying the alert message: \"Successfully registered\", after user data is stored in database, using MVC

I am using Asp.Net MVC3, C#, Enti

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-30 03:31

    Personally I'd go with AJAX.

    If you cannot switch to @Ajax... helpers, I suggest you to add a couple of properties in your model

    public bool TriggerOnLoad { get; set; }
    public string TriggerOnLoadMessage { get; set: }
    

    Change your view to a strongly typed Model via

    @using MyModel
    

    Before returning the View, in case of successfull creation do something like

    MyModel model = new MyModel();
    model.TriggerOnLoad = true;
    model.TriggerOnLoadMessage = "Object successfully created!";
    return View ("Add", model);
    

    then in your view, add this

    @{
       if (model.TriggerOnLoad) {
       
       
       
       }
    }
    

    Of course inside the tag you can choose to do anything you want, event declare a jQuery ready function:

    $(document).ready(function () {
       alert('@Model.TriggerOnLoadMessage');
    });
    

    Please remember to reset the Model properties upon successfully alert emission.

    Another nice thing about MVC is that you can actually define an EditorTemplate for all this, and then use it in your view via:

    @Html.EditorFor (m => m.TriggerOnLoadMessage)
    

    But in case you want to build up such a thing, maybe it's better to define your own C# class:

    class ClientMessageNotification {
        public bool TriggerOnLoad { get; set; }
        public string TriggerOnLoadMessage { get; set: }
    }
    

    and add a ClientMessageNotification property in your model. Then write EditorTemplate / DisplayTemplate for the ClientMessageNotification class and you're done. Nice, clean, and reusable.

提交回复
热议问题