-1

我已经被困在这一点上一段时间了。表格很好,但没有验证。此外,当我点击提交时,我收到错误:“加载部分视图脚本时出错(文件:~/Views/MacroPartials/ApplicationFormStub.cshtml)”

我不太确定我哪里出错了,非常感谢您对正确方向的建议,以使表单验证并至少通过。

这是我的模型:

[MetadataType(typeof(ApplicationMetaData))]
public partial class Application { }
public class ApplicationMetaData
{
    [DisplayName("Employer Name")]
    [Required (ErrorMessage="Please Enter the Employer's Name")]
    [StringLength(50, ErrorMessage = "Only 50 Characters allowed")]
    public string EmployerName { get; set; }

    [DisplayName("Supervisor Name")]
    [Required (ErrorMessage="Please Enter the Supervisor's Name")]
    [StringLength(50, ErrorMessage = "Only 50 Characters allowed")]
    public string SupervisorName { get; set; }

    [DisplayName("Supervisor Title")]
    [Required (ErrorMessage="Please Enter the Supervisor's Title")]
    [StringLength(50, ErrorMessage = "Only 50 Characters allowed")]
    public string SupervisorTitle { get; set; }

    [DisplayName("Employer Address")]
    [Required (ErrorMessage="Please Enter the Employer's Address")]
    [StringLength(50, ErrorMessage = "Only 50 Characters allowed")]
    public string EmployerAddress { get; set; }

    [DisplayName("Employer City")]
    [Required (ErrorMessage="Please Enter the Employer's City")]
    [StringLength(50, ErrorMessage = "Only 50 Characters allowed")]
    public string EmployerCity { get; set; }

    [DisplayName("Employer State")]
    [Required (ErrorMessage="Please Enter the Employer's State")]
    [StringLength(50, ErrorMessage = "Only 50 Characters allowed")]
    public object EmployerState { get; set; }

    [DisplayName("Employer Zip")]
    [Required (ErrorMessage="Please Enter the Employer's Zip")]
    [StringLength(50, ErrorMessage = "Only 50 Characters allowed")]
    public string EmployerZip { get; set; }

    [DisplayName("Employer Phone")]
    [StringLength(10, ErrorMessage = "Only 10 Characters allowed")]
    public string EmployerPhone { get; set; }
 }

宏部分文件:

@Html.Action("ShowApplication", "ServicesSurface")

表面控制器:

 [HttpGet]
    [ActionName("ShowApplication")]
    public ActionResult ShowApplication()
    {           
        return PartialView("ServicesApplicationForm", new ApplicationMetaData());
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    [ActionName("ShowApplication")]
    public ActionResult ShowApplication(ApplicationMetaData Model)
        {
         if (!ModelState.IsValid)
            {
                return CurrentUmbracoPage();
            }

         try
             {
              //Linq Data Entry
             }
         catch (Exception oe)
             {
             Response.Write(oe.Message);
             Response.End();
             }
         //Send Email 
         return RedirectToAction(RedirectUrl);
        }

看法:

@using Umbraco.Web
@using UmbracoProd.code
@model Umbraco.Services.ApplicationMetaData

<script src="@Url.Content("/../../scripts/libs/jquery-1.4.4.min.js")" type="text/javascript"></script>
<script src="@Url.Content("/../../scripts/libs/jquery/jquery.validate.min.js")" type="text/javascript"></script>
<link href="@Url.Content("/../../css/Services/form.css")" rel="stylesheet" type="text/css" />


@using (Html.BeginUmbracoForm("ShowApplication", "ServicesSurface"))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

<div class="FormArea">
    <fieldset>
        <legend>Section One</legend>
        <ul>
            <li>
                @Html.LabelFor(x => x.EmployerName)
                @Html.TextBoxFor(x => x.EmployerName)
            </li>
            <li>
                @Html.ValidationMessageFor(x => x.EmployerName)
            </li>
            <li>
                @Html.LabelFor(x => x.SupervisorName)
                @Html.TextBoxFor(x => x.SupervisorName)
            </li>
            <li>
                @Html.ValidationMessageFor(x => x.SupervisorName)
            </li>
            <li>
                @Html.LabelFor(x => x.SupervisorTitle)
                @Html.TextBoxFor(x => x.SupervisorTitle)
            </li>
             <li>
                @Html.ValidationMessageFor(x => x.SupervisorTitle)
            </li>
            <li>
                @Html.LabelFor(x => x.EmployerAddress)
                @Html.TextBoxFor(x => x.EmployerAddress)
            </li>
            <li>
                @Html.ValidationMessageFor(x => x.EmployerAddress)
            </li>
            <li>
                @Html.LabelFor(x => x.EmployerCity)
                @Html.TextBoxFor(x => x.EmployerCity)
            </li>
            <li>
                @Html.ValidationMessageFor(x => x.EmployerCity)
            </li>
            <li>
                @Html.LabelFor(x => x.EmployerState)                    
                @Html.StateDropDownList("EmployerState", "IN")
            </li>
            <li>
                @Html.ValidationMessageFor(x => x.EmployerState)
            </li>
            <li>
                @Html.LabelFor(x => x.EmployerZip)
                @Html.TextBoxFor(x => x.EmployerZip)
            </li>
            <li>
                @Html.ValidationMessageFor(x => x.EmployerZip)
            </li>
            <li>
                @Html.LabelFor(x => x.EmployerPhone)
                @Html.TextBoxFor(x => x.EmployerPhone)
            </li>
            <li>
                @Html.ValidationMessageFor(x => x.EmployerPhone)
            </li>                              
        </ul>
    </fieldset>        
  <input type="submit" class="button" value="Submit"/>
  </div> 
  }
4

2 回答 2

0

我收到一条错误消息,指出 ApplicationMetaData 没有 @Html.StateDropDownList() 的定义。

当我从表格中评论那一点时,我在进入控制器后又遇到了另一个错误,并且无法验证“只能在使用时在 Http POST 的上下文中使用 UmbracoPageResult”等。

我基本上从这里找到了我认为的答案:

在第二次运行Reset action的post后,由于modelstate被维护,通过传递一个新实例化的model,这个model会继承POST action中处理的model的model state(PostReset)。

在第二次调用重置操作期间,由于令牌数据已沿某处损坏,令牌验证失败并且它永远不会到达返回部分视图的地步。

即不要创建另一个模型发送相同的模型,我将 ShowApplication POST 中的失败验证代码更改为

    if (!ModelState.IsValid)
    {
        return PartialView("ServicesApplicationForm", Model);
    }

至少我回到了经过验证的表单,它正在验证,它不再出现在我的页面中,但我会把有趣的部分留给你。

于 2013-09-10T22:14:26.767 回答
0

很抱歉恢复旧线程,但由于数据库权限,我收到了类似的错误消息。

我启用了 SQL 服务器身份验证,并且我创建的用户没有对数据库的执行权限,我授予了权限并且错误消失了。

也许这会对某人有所帮助。

于 2016-01-27T15:22:57.320 回答