ASP.Net MVC显示后门柱特定HTML [英] ASP.Net MVC Showing specific html after post

查看:94
本文介绍了ASP.Net MVC显示后门柱特定HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还是很新的ASP.Net MVC,也许这是一个愚蠢的问题。

我使用MVC 3剃刀语法。

我的问题是,我怎么证明后,全成后一些HTML?

我有一个密码恢复页,并在发布正确的信息我想它再次显示视图,但增加一个新密码已发送到您的电子邮件地址来了。

在的WebForms我只希望有一个无形的面板,显示它或文字和东西一些文本它回发。我怎么在MVC办?

我想我可以添加不同的视图,但确实似乎有点矫枉过正一样 - 或者我在这里误解MVC范例

编辑:

我试图用这个强类型的模型,但它不工作: - (

我的观点(简化了一下):

  @model BudgetPal.Model.MVC.Account.EditModel
@if(Model.ShowConfirmation)
{
    < D​​IV CLASS =确认方式>您的个人资料已经保存< / DIV>
}

示范:

 公共类RecoverPasswordModel
{
    [需要]
    [显示(名字=电子邮件)]
    [StringLength(250)]
    [RegularEx$p$pssion(@\"([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})\",的ErrorMessage =不是一个有效的电子邮件地址)]
    公共字符串电子邮件{获得;组; }    公共BOOL ShowConfirmation {搞定;组; }
}

和我的控制器:

 公众的ActionResult编辑()
        {
            EditModel模式=新EditModel(UserSession.User);
            返回查看(模型);
        }        [HttpPost]
        公众的ActionResult编辑(EditModel模型)
        {
            如果(!ModelState.IsValid)
                返回查看(模型);            //保存更改这里            model.ShowConfirmation = TRUE;
            返回查看(模型);
        }


解决方案

您不必添加一个单独的视图。快速的解决办法是设置动态ViewBag对象上的属性,并检查其是否您的视图中的存在。

在你的控制器,设置动态ViewBag财产...

  ViewBag.Message =新密码已发送到您的电子邮件地址。

在你看来,像...

  @if(ViewBag.Message!= NULL){
    < D​​IV CLASS =消息> @ ViewBag.Message< / DIV>
}

您可以找到一些更多的信息<一个href=\"http://weblogs.asp.net/hajan/archive/2010/12/11/viewbag-dynamic-in-asp-net-mvc-3-rc-2.aspx\">here.

如果您使用的是强类型的视图,你可以简单地将属性添加到您的视图模型对象将存储信息,并检查它以同样的方式,你检查动态ViewBag属性来确定是否显示该消息。

I'm still quite new to ASP.Net MVC, so maybe this is a stupid question.

I'm using MVC 3 with razor syntax.

My question is, how do I show some HTML after a successfull post ?

I've got a password recovery page, and upon posting correct information I'd like it to show the view again, but add a "A new password has been sent to your e-mail address" to it.

In WebForms I'd just have a invisible panel and show it, or a literal and stuff some text into it on postback. What do I do in MVC ?

I suppose I could add a different view for it, but really that seems a bit like overkill - or am I misunderstand the MVC paradigm here ?

EDIT:

I tried using the strongly typed model for this, but it doesn't work :-(

My view (simplified a bit):

@model BudgetPal.Model.MVC.Account.EditModel
@if (Model.ShowConfirmation)
{
    <div class="confirmation">Your profile has been saved.</div>
}

The Model:

public class RecoverPasswordModel
{
    [Required]
    [Display(Name = "E-mail")]
    [StringLength(250)]
    [RegularExpression(@"([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})", ErrorMessage = "Not a valid e-mail address")]
    public string Email { get; set; }

    public bool ShowConfirmation { get; set; }
}

And my controller:

public ActionResult Edit()
        {
            EditModel model = new EditModel(UserSession.User);
            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(EditModel model)
        {
            if (!ModelState.IsValid)
                return View(model);

            // Save changes here

            model.ShowConfirmation = true;
            return View(model);
        }

解决方案

You don't need to add a separate view. The quick solution would be to set a property on the dynamic ViewBag object and check for its existence inside your view.

In your controller, set the dynamic ViewBag property...

ViewBag.Message = "A new password has been sent to your email address.";

In your view, something like...

@if(ViewBag.Message != null) {
    <div class="message">@ViewBag.Message</div>
}

You can find some more information here.

If you are using a strongly-typed view, you can simply add a property to your view model object that will store the message, and check it in the same manner you check the dynamic ViewBag property to determine whether or not to display the message.

这篇关于ASP.Net MVC显示后门柱特定HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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