【问题标题】:Partial View validation with composite view model使用复合视图模型进行局部视图验证
【发布时间】:2012-11-05 13:15:36
【问题描述】:

我有一个有两种形式的屏幕。一个允许登录站点,另一个允许登录 ftp。

登录视图使用 WelcomeScreenViewModel(组合模型)进行强类型化。 每个表单都是一个强类型的局部视图。

这是类定义。

public class LogOnViewModel
    {
        [LocalizedDisplayNameAttribute("username", typeof(MyLabels.labels))]
        [Required]
        public string UserName { get; set; }
        [LocalizedDisplayNameAttribute("password", typeof(MyLabels.labels))]
        [Required]
        public string Password { get; set; }
    }

    public class FTPViewModel
    {
        [LocalizedDisplayNameAttribute("username", typeof(MyLabels.labels))]
        [Required]
        public string UserName { get; set; }
        [LocalizedDisplayNameAttribute("password", typeof(MyLabels.labels))]
        [Required]
        public string Password { get; set; }
    }

    public class WelcomeScreenViewModel
    {
        public LogOnViewModel LogOnModel { get; set; }
        public FTPViewModel FTPModel { get; set; }
    }

我的主页继承了 WelcomeScreenViewModel,并且我这样渲染部分视图:

Html.RenderPartial("登录", Model.LogOnModel);

Html.RenderPartial("FTP", Model.FTPModel);

我的控制器代码:

// To display blank login on load of page
public ActionResult Login(string language)
        {
            WelcomeScreenViewModel combined = new WelcomeScreenViewModel();
            combined.FTPModel = new FTPViewModel();
            combined.LogOnModel = new LogOnViewModel();
            return View(combined);
        }

// Called when clicking submit on first form
[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Logon(string language, LogOnViewModel logon)
        {
            WelcomeScreenViewModel combined = new WelcomeScreenViewModel();
            combined.FTPModel = new FTPViewModel();
            combined.LogOnModel = logon;

            if (!ModelState.IsValid)
            {
                ViewData["result"] = "Invalid login info / Informations de connexion incorrectes";

                // This is the part I can't figure out.  How do I return page with validation summary errors
                return View(logon);
            }
            else
            {
                ...
            }            
        }

到目前为止,我的问题是当我的 ModelState 无效时要返回什么。如何返回包含验证摘要错误的页面?上面显示的代码只返回部分视图表单(不在主视图内),没有验证。我究竟做错了什么?我开始使用this post,但它没有显示足够的代码来帮助我。

任何帮助将不胜感激。谢谢。

【问题讨论】:

    标签: c# .net asp.net-mvc-3 validation model


    【解决方案1】:

    我的代码有 2 个问题。

    1) 两个子模型的每个字段必须有不同的名称。

    public class LogOnViewModel
        {
            [LocalizedDisplayNameAttribute("username", typeof(MyLabels.labels))]
            [Required]
            public string UserName { get; set; }
            [LocalizedDisplayNameAttribute("password", typeof(MyLabels.labels))]
            [Required]
            public string Password { get; set; }
        }
    
        public class FTPViewModel
        {
            [LocalizedDisplayNameAttribute("username", typeof(MyLabels.labels))]
            [Required]
            public string ftpUserName { get; set; }
            [LocalizedDisplayNameAttribute("password", typeof(MyLabels.labels))]
            [Required]
            public string ftpPassword { get; set; }
        }
    

    2) 这是用于返回验证和值的代码:

    return View("~/Views/Login/Login.aspx",combined);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-18
      相关资源
      最近更新 更多