Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET MVC验证错误未显示_C#_Asp.net_Asp.net Mvc_Validation_Asp.net Ajax - Fatal编程技术网

C# ASP.NET MVC验证错误未显示

C# ASP.NET MVC验证错误未显示,c#,asp.net,asp.net-mvc,validation,asp.net-ajax,C#,Asp.net,Asp.net Mvc,Validation,Asp.net Ajax,我正在使用元组将多个模型加载到一个视图中 观点如下 @using (Ajax.BeginForm(null, null, ajaxOptions, new { @class = "contact-form" })) { @Html.AntiForgeryToken() <div class="form-group form-group--xs"> @Html.TextBoxFor(m =>

我正在使用
元组
将多个模型加载到一个视图中

观点如下

 @using (Ajax.BeginForm(null, null, ajaxOptions, new { @class = "contact-form" }))

        {
           @Html.AntiForgeryToken()
           <div class="form-group form-group--xs">
              @Html.TextBoxFor(m => m.Item2.Email, new { @class = "form-control input-sm", placeholder = "Your email address..." })
              @Html.ValidationMessageFor(m => m.Item2.Email)
           </div>
           <div class="form-group form-group--xs">
              @Html.TextAreaFor(m => m.Item2.Message, new { @class = "form-control input-sm", placeholder = "Your message...", rows = "4" })
              @Html.ValidationMessageFor(m => m.Item2.Message)
           </div>

           <input type="submit" class="btn btn-primary-inverse btn-sm btn-block" value="Send Your Message" />
        }
最后是局部观点

@model bool

@if (Model)
{
    <div style="color:#c2ff1f">Your message has been sent.</div>
}
else
{
    <div style="color:#f34141">An error occured while sending your message</div>
}
@modelbool
@if(型号)
{
您的邮件已发送。
}
其他的
{
发送邮件时出错
}
更新:电子邮件文本框的呈现html为

<input class="form-control input-sm" 
data-val="true" 
data-val-email="The Email field is not a valid e-mail address." 
data-val-required="You must provide your email." 
id="Item2_Email" 
name="Item2.Email" 
placeholder="Your email address..." 
type="text" 
value="">

如果我没有在电子邮件字段或消息字段中输入任何文本,则不会发送消息:),但我在视图中没有验证错误:(
我在这里做错了什么?

您返回的partialView没有验证错误消息代码

请尝试以下代码:

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<PartialViewResult> Submit([Bind(Prefix = "Item2")] ContactForm model)
        {
            bool isMessageSent = true;

            if (ModelState.IsValid)
            {
                try
                {
                    await Services.EmailService.SendContactForm(model);
                }
                catch (Exception ex)
                {
                    isMessageSent = false;
                }

            }
            else
            {
                isMessageSent = false;
                //Return the view that has validation error message display code.
                return View(model);
            }
            return PartialView("_SubmitMessage", isMessageSent);
        }
[HttpPost]
[ValidateAntiForgeryToken]
公共异步任务提交([Bind(Prefix=“Item2”)]ContactForm模型)
{
bool isMessageSent=true;
if(ModelState.IsValid)
{
尝试
{
wait Services.EmailService.SendContactForm(模型);
}
捕获(例外情况除外)
{
isMessageSent=假;
}
}
其他的
{
isMessageSent=假;
//返回包含验证错误消息显示代码的视图。
返回视图(模型);
}
返回部分视图(“提交消息”,isMessageSent);
}

我想我需要休息一下。我失踪了

@Scripts.Render("~/bundles/jqueryval")

对于像我这样的人,只需将以下脚本添加到您的视图中:


  • 我不确定,但我认为如果您返回一个视图而不是partialview会更好。您将进入屏幕,屏幕上显示发送电子邮件时发生了错误。我认为在控制器的else块中插入“return view();”就够了。@Larce改为公共异步任务并返回视图(模型);但它也没有按预期工作发生了什么?您仍然可以访问部分视图吗?我的意思是验证错误没有显示,我现在没有返回部分视图。我猜元组onesHello出现问题,我可以从同一个控制器返回PartialViewResult和ViewResult吗?如果您返回actionResult.Yes!@OrElse更新中的html,代码是在点击提交之前生成的还是在点击提交之后生成的(返回时有错误)?@Kumar_Vikas我们不能从同一个控制器同时返回PartialViewResult和ViewResult。@ParthRuparelia为什么不能?
            [HttpPost]
            [ValidateAntiForgeryToken]
            public async Task<PartialViewResult> Submit([Bind(Prefix = "Item2")] ContactForm model)
            {
                bool isMessageSent = true;
    
                if (ModelState.IsValid)
                {
                    try
                    {
                        await Services.EmailService.SendContactForm(model);
                    }
                    catch (Exception ex)
                    {
                        isMessageSent = false;
                    }
    
                }
                else
                {
                    isMessageSent = false;
                    //Return the view that has validation error message display code.
                    return View(model);
                }
                return PartialView("_SubmitMessage", isMessageSent);
            }
    
    @Scripts.Render("~/bundles/jqueryval")