ASP.NET MVC中的验证局部视图并返回到父视图 [英] ASP.NET MVC Validation in Partial View and return to Parent view

查看:99
本文介绍了ASP.NET MVC中的验证局部视图并返回到父视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的背景

我在第一次严重的项目中使用ASP.NET MVC 4.工作我的工作,因为传统的ASP天Web开发,并且得到了很好的Web表单搁置。 MVC是非常令人兴奋和我做了良好进展。但现在我在一个情况下,我需要在这个论坛上提供帮助。

查询后台

我有一个父视图和里面有一个局部视图。局部视图包含一个表单并提交按钮。局部视图都有自己的本地视图模型和视图模型是父视图模型的属性之一。

在情况下,偏看法验证失败,我想,展现了父视图,因为它是和突出局部视图无效的领域。

在code未破的任何地方,但是当有一个验证错误,不知何故,我没有找到展示与传递给它的初始化模型父视图正道。当然,要突出在局部视图中的错误。

任何帮助将是AP preciated。谢谢你。

code看起来像下面这样:

查看模式:

 公共类ParentViewModel
{
    公众诠释TitleId {搞定;组; }
    公共字符串名称{;组; }
    公共ChildViewModel儿童{搞定;组; }
}公共类ChildViewModel
{
    [需要]
    公共小数评级{搞定;组; }    [需要]
    [StringLength(500)]
    [显示(说明=评论)
    [RegularEx pression(([A-ZA-Z0-9&放大器;' - ] +)的ErrorMessage =只能输入字母和名字的数字)
    公共字符串ReviewText {搞定;组; }
}

控制器

 公共类的TestController:控制器
{
    公众的ActionResult指数()
    {
        VAR模型=新ParentViewModel()
                        {
                            TitleId = 1,名称=父名,
                            儿童=新ChildViewModel()
                                        {
                                            等级= 2.5M,ReviewText =它必须看!
                                        }
                        };
        返回查看(「指数」,型号);
    }    [HttpPost]
    公众的ActionResult SubmitReview(ChildViewModel模型)
    {
        如果(ModelState.IsValid)
        {
            返回视图(_子,模型);
        }        ModelState.AddModelError(,一些错误。);
        返回视图(_子,模型);
    }
}

父视图

  @model ParentViewModel
@ {
    布局= NULL;
}
<!DOCTYPE HTML>
< HTML和GT;
< HEAD>
    <标题>指数< /标题>
< /头>
<身体GT;
    < D​​IV>
        @ Model.TitleId,@ Model.Name
    < / DIV>
    < D​​IV>
        @ Html.Partial(_子,Model.Child)
    < / DIV>
< /身体GT;
< / HTML>

局部视图(_child.cshtml)

  @model ChildViewModel
@using(Html.BeginForm(SubmitReview,测试))
{
    @ Html.ValidationSummary(真)    @ Html.TextBoxFor(M = GT; m.Rating)@ Html.ValidationMessageFor(M = GT; m.Rating)
    @ Html.TextBoxFor(M = GT; m.ReviewText)@ Html.ValidationMessageFor(M = GT; m.ReviewText)
    <输入类型=登陆/&GT提交值=;
}


解决方案

您需要显示父视图不是孩子之一,所以动作应该是这样的:

  [HttpPost]
    公众的ActionResult SubmitReview(ChildViewModel模型)
    {
             VAR parentViewModel =写的init code在这里;
            parentViewModel.ChildModel =模型;        如果(ModelState.IsValid)
        {            返回视图(指数,parentViewModel);
        }        ModelState.AddModelError(,一些错误。);
        返回视图(指数,parentViewModel);
    }

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. MVC is very exciting and am doing good progress. 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)

@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 );
    }

这篇关于ASP.NET MVC中的验证局部视图并返回到父视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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