简体   繁体   English

成功消息作为ASP.NET MVC中的模型

[英]Success Message as a model in ASP.NET MVC

I intend to reuse success message in my MVC web application as a model, and this model could be a part of another model to report feedback status during data modification. 我打算在我的MVC Web应用程序中将成功消息作为模型重用,并且该模型可以成为另一个模型的一部分,以在数据修改期间报告反馈状态。 So far, I can't get this working. 到目前为止,我无法正常工作。 Say I have FeedbackMessage object as model below to reflect success message: 假设我在下面的模型中有FeedbackMessage对象,以反映成功消息:

public class FeedbackMessage
{
    public string Message { get; private set; }
    public FeedbackType Type { get; private set; } // 'info', 'error', 'warning', 'success'
    public string Image { get; }
}

and I have a user preference page which uses Preference object as the model and this object has FeedbackMessage as folowing 并且我有一个用户首选项页面,该页面使用Preference对象作为模型,并且该对象具有以下的FeedbackMessage

public class Preference
{
    public string DisplayName { get; set;}
    public string BlogUrl { get; set; }
    public FeedbackMessage FeedbackMessage { get; set; }
    public bool IsValid { get; set; }
}

and in my controller, I follow PRG (Post-Redirect-Get) pattern for data modification. 在我的控制器中,我遵循PRG(后重定向获取)模式进行数据修改。 The code is briefly as following: 代码简要如下:

public ActionResult Preference()
{
     var model = new Preference();
     model.DisplayName = Models.Settings.Preference.DisplayName;
     ...
     return this.View(model);
}

[HttpPost]
public Action Preference(Preference model)
{
    if (model.IsValid)
    { 
         model.FeedbackMessage = new FeedbackMessage { Type = "success" };
    }
    else
    {
         model.FeedbackMessage = new FeedbackMessage { Type = "failure" };
    }

    ...

    return this.Preference(); // issue Get
}

My problem is that I can't get feedback at all after doing POST, since during GET it again initializes Preference object. 我的问题是在执行POST之后根本无法获得反馈,因为在GET期间它再次初始化了Preference对象。 At this point, I'm reluctant to use TempData as suggested by other stackoverlow thread since it's more explicit and cleaner done in the model. 在这一点上,我不愿意像其他stackoverlow线程所建议的那样使用TempData,因为它在模型中更加明确和简洁。

Could anyone that has experience around this area show/guide me how to fix this or else show me a better design? 可以在这个领域有经验的人向我展示/指导我如何解决此问题,或者向我展示更好的设计吗? Thanks. 谢谢。

  1. Use ajax to post the preference model, then use json to return the result(FeedbackMessage) to client, and use js to display the feedback 使用ajax发布首选项模型,然后使用json将结果(FeedbackMessage)返回给客户端,并使用js显示反馈

  2. Because Post-Redirect-Get across different request, Normally use TempData to save model and display in the next request, but we can do this at base layout view 由于Post-Redirect-Get跨不同的请求,因此通常使用TempData保存模型并在下一个请求中显示,但是我们可以在基本布局视图中执行此操作

  3. If you don't want to change the code above, you can add a private method whith accept different perfenrence model and return the view action to client. 如果您不想更改上面的代码,则可以添加一个接受其他性能模型的私有方法,然后将view操作返回给客户端。 so that reuse the model 这样就可以重用模型

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

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