简体   繁体   English

部分视图中的ASP.NET MVC验证并返回到父视图

[英]ASP.NET MVC Validation in Partial View and return to Parent view

My Background 我的背景

I am working on first serious project using ASP.NET MVC 4. I am working on web development since classic ASP days and have got good hold on Webforms. 我正在使用ASP.NET MVC 4开展第一个认真的项目。自从经典的ASP时代以来,我正致力于Web开发,并且已经很好地掌握了Webforms。 MVC is very exciting and am doing good progress. MVC非常令人兴奋,并且正在取得良好进展。 But now I am in a situation where I need help on this forum. 但现在我处在这个论坛上我需要帮助的情况。

Query background 查询背景

I have a parent view and inside it there is a partial view. 我有一个父视图,里面有一个局部视图。 Partial view contains a form and submit button. 部分视图包含表单和提交按钮。 The partial view has its own local view model and that view model is one of the properties of the Parent view model. 局部视图具有自己的局部视图模型,该视图模型是父视图模型的属性之一。

In case the validations on partial views fail, I want to, show the parent view as it is and highlight invalid fields in partial view. 如果部分视图的验证失败,我想,按原样显示父视图并在部分视图中突出显示无效字段。

The code is not breaking anywhere but when there is a validation error, somehow, i am not finding right way to show parent view with initialised model passed to it. 代码没有破坏任何地方,但是当存在验证错误时,不知何故,我找不到正确的方式来显示传递给它的初始化模型的父视图。 And of course, to highlight the errors in partial view. 当然,要突出部分视图中的错误。

Any help would be appreciated. 任何帮助,将不胜感激。 Thanks. 谢谢。

Code looks like following: 代码如下所示:

View Models: 查看型号:

public class ParentViewModel
{
    public int TitleId { get; set; }
    public string Name { get; set; }
    public ChildViewModel Child { get; set; }
}

public class ChildViewModel
{
    [Required]
    public decimal Rating { get; set; }        

    [Required]
    [StringLength(500)]
    [Display(Description = "Review")]
    [RegularExpression("([a-zA-Z0-9 .&'-]+)", ErrorMessage = "Enter only alphabets and numbers of First Name")]
    public string ReviewText { get; set; }
}

Controller 调节器

public class TestController : Controller
{
    public ActionResult Index()
    {
        var model = new ParentViewModel()
                        {
                            TitleId = 1,Name = "Parent name",
                            Child = new ChildViewModel()
                                        {
                                            Rating = 2.5M, ReviewText = "Its a must watch!"
                                        }
                        };
        return View("Index", model);  
    }

    [HttpPost]
    public ActionResult SubmitReview(ChildViewModel model)
    {
        if (ModelState.IsValid )
        {
            return View("_child", model);
        }

        ModelState.AddModelError("", "Some Error.");
        return View("_child", model);
    }
}

Parent View 父视图

@model ParentViewModel
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <title>Index</title>
</head>
<body>
    <div>
        @Model.TitleId, @Model.Name        
    </div>
    <div>
        @Html.Partial("_child", Model.Child)
    </div>
</body>
</html>

Partial View (_child.cshtml) 部分视图(_child.cshtml)

@model ChildViewModel
@using (Html.BeginForm("SubmitReview", "Test"))
{
    @Html.ValidationSummary(true)

    @Html.TextBoxFor(m => m.Rating)     @Html.ValidationMessageFor(m => m.Rating)
    @Html.TextBoxFor(m => m.ReviewText) @Html.ValidationMessageFor(m => m.ReviewText)
    <input type="submit" value="Log in" />
}

You need to show the parent view not the child one, so the action should look like: 您需要显示父视图而不是子视图,因此操作应如下所示:

[HttpPost]
    public ActionResult SubmitReview(ChildViewModel model)
    {
             var parentViewModel = write init code here;
            parentViewModel.ChildModel = model;

        if (ModelState.IsValid )
        {

            return View("Index", parentViewModel );
        }

        ModelState.AddModelError("", "Some Error.");
        return View("Index", parentViewModel );
    }

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

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