2016-01-27 103 views
2

我想表明成功的消息時,插入是complete.Here插入後如何顯示成功消息是我的代碼通過形式提交

[HttpPost] 
public ActionResult Create(string txthospitalname, int city) 
{ 
    var hospitals = new Hospital() 
    { 
     Name = txthospitalname, 
     FKCityID = city, 
     CreatedAt = DateTime.Now, 
     CreatedBy = 1, 
    }; 
    _db.Hospitals.Add(hospitals); 
    _db.SaveChanges(); 
    return RedirectToAction("Create"); 
} 

View.cshtml

@using (Html.BeginForm("Create", "Hospital", FormMethod.Post, new {enctype = "multipart/form-data", id = "hospitalform"})) 
{ 

    //Text Fields 

} 

回答

1

使用TempData是理想的Post-Redirect-Get模式。

您的文章操作:

[HttpPost] 
public ActionResult Create(string txthospitalname, int city) 
{ 
    var hospitals = new Hospital() 
    { 
     Name = txthospitalname, 
     FKCityID = city, 
     CreatedAt = DateTime.Now, 
     CreatedBy = 1, 
    }; 
    _db.Hospitals.Add(hospitals); 
    _db.SaveChanges(); 
    TempData["Success"] = true; 
    return RedirectToAction("Create"); 
} 

的GET操作:

[HttpGet] 
public ActionResult Create() 
{ 
    ViewBag.Success = TempData["Success"] as bool; 
    return View(); 
} 

您的看法:

@if (ViewBag.Success != null && ViewBag.Success) 
{ 
    <h2> Your Success Message Here</h2> 
} 

@using (Html.BeginForm("Create", "Hospital", FormMethod.Post, 
    new {enctype = "multipart/form-data", id = "hospitalform"})) 
{ 
    //Text Fields 
}  
+0

$(文件)。就緒(函數(){ 如果(」 @ ViewBag.Success'==「Added」){ toastr.success(「Hospital added successfull」); } }); –

+0

在腳本中添加此代碼後,它爲我工作就像一個魅力..謝謝親愛的。 –

+0

很高興幫助。 –