如何在MVC中显示save succeessfull消息? [英] How to display the save succeessfull message in MVC ?

查看:68
本文介绍了如何在MVC中显示save succeessfull消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 [HttpPost] 
[MenuAuthorize( 82B5690B-BEA7- 40EC-9643-E4E37431833B)]
public ActionResult RoundOff(RoundoffSetting model)
{
尝试
{
var item =( from a wallet.RoundoffSettings 其中 a.CompanyId == CompanyId 选择 a).FirstOrDefault();

if (item == null
{
item = new RoundoffSetting();
item.CompanyId = CompanyId ?? 0 ;
item.CreatedBy = User.Identity.Name;
item.CreatedDate = DateTime.Now;

wallet.RoundoffSettings.Add(item);
}
else
{
item.ModifiedBy = User.Identity.Name;
item.ModifiedDate = DateTime.Now;
}

item.RoundingType = model.RoundingType;
item.RoudingValue = model.RoudingValue;

wallet.SaveChanges();

return 成功(M( RoundOffSaveSucess));
}
catch (例外e)
{
log.Error( 无法保存RoundOff,e);
return 错误();
}
}





我的尝试:



我已经给出了保存动作的代码......在此,保存数据后,一些成功的消息必须显示..在这段代码中,我已经在那个地方写了返回成功我通过了一个你看过的消息。保存表格后只显示消息....如果有人知道,请帮助我请分享代码请帮帮我

解决方案

您好,



首先在控制器动作中,使用上下文或数据层内部动作方法是一种不好的方法。



接下来是注释部分正确提到的Success方法,为什么它是必需的,当你可以在任何模型属性中存储Success消息时,如果你从Action返回一个视图,理想情况下你应该这样做。如果你返回一个视图,你总是可以创建一个名为ValidationMessage的独立Model属性,或者使用ViewBag,tempdata这样的任何东西。



如果你正在使用任何ajax调用要调用这个Post方法,那么从方法中根据ajax成功部分中的响应操作进行布尔返回会很简单。



谢谢

[HttpPost]
[MenuAuthorize("82B5690B-BEA7-40EC-9643-E4E37431833B")]
public ActionResult RoundOff(RoundoffSetting model)
{
    try
    {
        var item = (from a in wallet.RoundoffSettings where a.CompanyId == CompanyId select a).FirstOrDefault();

        if (item == null)
        {
            item = new RoundoffSetting();
            item.CompanyId = CompanyId ?? 0;
            item.CreatedBy = User.Identity.Name;
            item.CreatedDate = DateTime.Now;

            wallet.RoundoffSettings.Add(item);
        }
        else
        {
            item.ModifiedBy = User.Identity.Name;
            item.ModifiedDate = DateTime.Now;
        }

        item.RoundingType = model.RoundingType;
        item.RoudingValue = model.RoudingValue;

        wallet.SaveChanges();

        return Success(M("RoundOffSaveSucess"));
    }
    catch (Exception e)
    {
        log.Error("Failed to save RoundOff", e);
        return Error();
    }
}



What I have tried:

I have given code for saving Action...in this ,After save the data some successfull message has to display .. in this code i had wrote return success in that place i passed one message you seen.that message only display after am saving my form ....please help me if anyone know this please share the code please help me

解决方案

Hello,

First of all in the controller action, using the context or Data layer inside action method is a bad approach.

Next is the Success method as rightly mentioned in the Comments section, why is it required, when you can store the Success message in any model property, if you are returning a view from the Action, which ideally you should. if you return a view, you can always create a separate Model property named ValidationMessage, or use ViewBag, tempdata anything of that sort.

If you are using any ajax call to call this Post method, then that would be simple to make a boolean return from the method and based on the response manipulate in the ajax success part.

Thanks


这篇关于如何在MVC中显示save succeessfull消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆