English 中文(简体)
流利摘要总是显示
原标题:FluentValidation summary always displaying

使用电解法的Im似乎在装货时展示。 我没有任何东西可以自动提交表格,在我的控制人员中,在表格公布之前,没有进行验证检查。 充其量,验证似乎在提交时完美无缺。

观点:

[Validator(typeof(SendMessageInputValidator))]
public class SendMessageInput
{
    public string Title { get; set; }
    public string Content { get; set; }
    public string VideoUrl { get; set; }
    public string CultureName { get; set; }
    public bool VideoMode { get; set; }
}

public class SendMessageInputValidator : AbstractValidator<SendMessageInput>
{
    public SendMessageInputValidator()
    {
        RuleFor(s => s.Title)
            .NotEmpty().WithMessage("TitleRequired".Translate("MCN"));
    }
}

主计长:

    public ActionResult Detail(Guid entityId, string cultureName)
    {
        var entity = _sendMessageRepository.Get(entityId);

        if (entity == null)
            throw new HttpException(404, "Not found.");

        return View(new SendMessagePageViewModel
                        {
                            NodeId = entity.NodeId,
                            Name = entity.Name,
                            Title = entity.Title,
                            Content = entity.Content,
                            BrowserTitle = entity.BrowserTitle,
                            MetaDescription = entity.MetaDescription,
                            MetaKeywords = entity.MetaKeywords,
                            SendMessageInput = new SendMessageInput { VideoMode = true }
                        });
    }

    public ActionResult SendMessageForm(SendMessageInput input)
    {
        input.CultureName = Thread.CurrentThread.CurrentUICulture.Name;
        return PartialView(/*input*/ new SendMessageInput());
    }

    [HttpPost]
    public ActionResult SendMessage(SendMessageInput input)
    {
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(input.CultureName);

        if (ModelState.IsValid)
        {
            return Redirect(Utilities.GetUrl(Constants.NodeIds.MyProfile));
        }

        var entity = _sendMessageRepository.Get(Constants.NodeIds.MentorQuestionForm);

        if (entity == null)
            throw new HttpException(404, "Not found.");

        return PartialView("Detail", new SendMessagePageViewModel
                                         {
                                             NodeId = entity.NodeId,
                                             Name = entity.Name,
                                             Title = entity.Title,
                                             Content = entity.Content,
                                             BrowserTitle = entity.BrowserTitle,
                                             MetaDescription = entity.MetaDescription,
                                             MetaKeywords = entity.MetaKeywords,
                                             SendMessageInput = input
                                         });
    }

观点(主要):

 @Html.Action("SendMessageForm", "SendMessage", Model.SendMessageInput)

观点(部分):

@Html.ValidationSummary(false, "ValidationSummaryHeader".Translate("MCN"))  
@using (Html.BeginForm("SendMessage", "SendMessage", FormMethod.Post))
{
    <div class="Formulaire">
        <p>
            @Html.LabelFor(m => m.Title, "Title".Translate("MCN"), true)
            @Html.TextBoxFor(m => m.Title, new { maxlength = 200, @class = "TxtBox" })
        </p>

        @if (Model.VideoMode)
        {
            <p>
                @Html.LabelFor(m => m.VideoUrl, "VideoUrl".Translate("MCN"))
                @Html.TextBoxFor(m => m.VideoUrl)
            </p>
        }
        else
        {
            <p>
                @Html.LabelFor(m => m.Content, "Message".Translate("MCN"))
                @Html.TextAreaFor(m => m.Content, new { @class = "TxtArea" })
            </p>
        }

        @Html.HiddenFor(m => m.CultureName)

        <input type="submit" value="@("Submit".Translate("MCN"))"/>
    </div>
}
最佳回答

我认为,在你首次展示“详细观点”时,你正在推出一个<条码>,即“SendMessageInput,因为新版将有一个空洞的<条码>Title。

当你通过<代码>时 页: 1 因此,在具有约束力的模型中,它将出现一种模型错误,因此,当你的部分观点得到体现时,将展示验证摘要。

您是否使用<代码>Html.Partial(或Html.Render Partial代替 Html.Action? 这将使形式不致出现任何模型。

问题回答

即便回答是回答的一部分,我也未能做到这一点。

因此,对于今后读写这句话的人来说,我是如何做到的:

  • @Html.ValidationSummary() must be in the @using statement or it just doesn t show the validation summary.
  • Using @Html.Partial or @Html.RenderPartial is considered a good pratice but won t prevent the summary to be displayed.
  • I did solve my problem though using CSS. The validation summary is first rendered with a class validation-summary-valid and I applied a display:none to it. Once there are errors, the class is changed to validation-summary-errors.

在那里,你拥有这种权力,甚至连流利也无关紧要,这只是Html延伸工作的方式。





相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签