使用 xVal 和 ViewModel

发布于 2024-08-07 21:12:05 字数 3678 浏览 5 评论 0原文

我看到一篇类似的文章试图用晚餐示例做同样的事情,但他解决了他的问题,我的问题似乎更深一些。基本上我 cxan 验证工作得很好,但它只适用于 Firefox。在 IE7 中,当页面加载时,我立即收到一个警告框,其中包含以下消息:“错误:元素标题不在表单中”...显然它是在此处的表单中,如果需要,我可以发布实际呈现的标记从查看源代码来显示这一点。任何关于我能做什么来解决这个问题的想法将不胜感激!

基本上我只是想确保我的 NewsPost 有标题和正文。因为我把它包含在 ViewModel 中,所以我认为 IE 不太理解这一点。也许我错了。

我正在使用 xVal 进行验证。我正在传递一个 ViewModel 作为我的模型。我的 ViewModel 看起来像这样:

public class NewsAdminViewData : ViewModel
{
   public NewsPost NewsPost { get; set; }
   public List<SelectListItem> NewsItem { get; set; }
   public List<SelectListItem> NewsGroup { get; set; }

   public NewsAdminViewData(List<SelectListItem> newsItem, List<SelectListItem> newsGroup, NewsPost newsPost)
   {
      this.NewsItem = newsItem;
      this.NewsGroup = newsGroup;
      this.NewsPost = newsPost;
   }
}

这是我的视图:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MVCApp.Models.ViewModels.News.NewsAdminViewData>" %>

    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
        <% using (Html.BeginForm())
           {%>
        <div class="moduleContainer">
            <div class="moduleTitle">
                Create News Item
            </div>
            <div class="moduleContent">
                <div>
                    <div>
                        Title:</div>
                    <div>
                        <%= Html.TextBox("Title") %>
                    </div>
                </div>
                <div>
                    <div>
                        &nbsp;</div>
                    <div>
                        <%= Html.TextArea("Body") %>
                    </div>
                </div>
                <div>
                    <div>
                        News Group:
                    </div>
                    <div>
                        <%= Html.DropDownList("NewsGroup")%>
                    </div>
                </div>
                <div>
                    <div>
                        News Item:
                    </div>
                    <div>
                        <%= Html.DropDownList("NewsItem") %>
                    </div>
                </div>
            </div>
            <div class="moduleFooter">
                <%= Html.SubmitButton("btnSubmit", "Add News Post", null, "To add this news post click here.", "#ffd40f")%>
            </div>
        </div>
        <% } %>
        <%= Html.ClientSideValidation<NewsPost>()%>

最后是我的后操作:

 [AcceptVerbs(HttpVerbs.Post)]
    public virtual ActionResult Create(/*FormCollection collection*/ NewsPost np)
    {
       NewsPost entity = this.reposNewsPost.New();
       try
       {
          entity.Title = np.Title; 
          entity.NewsPostGUID = System.Guid.NewGuid();
          entity.DateAdded = DateTime.Now;
          entity.DateUpdated = DateTime.Now;
          entity.Body = np.Body; 

          UpdateModel(entity);
          this.reposNewsPost.Insert(entity);
          this.reposNewsPost.SubmitChanges();
          return RedirectToAction("Index");
        }
        catch (RulesException ex)
        {
           ex.AddModelStateErrors(ModelState, "NewsPost");
           return ModelState.IsValid ? RedirectToAction(MVC.News.Actions.Create) 
                        : (ActionResult)View();
        }
     }

I saw a similar post that was trying to do this same thing with the Dinner example, but he fixed his issue mine seems to be a little deeper. Basically I cxan get the validation to work just fine but it only works in Firefox. In IE7 when the page loads, I immediately get an alert box with the following message: "Error: Element title is not in a form"... Clearly it is in form here, If needed i can post the markup that is actually rendered from view source to show this. Any ideas on what I can do to fix this would be most appreciated!

Basically I am just trying to make sure my NewsPost has a title and a body. Since I have it wrapped up in the ViewModel I'm thinking IE doesnt quite understand this. Maybe I am wrong.

I am using xVal for my validation. I am passing a ViewModel in as my Model. My ViewModel looks like this:

public class NewsAdminViewData : ViewModel
{
   public NewsPost NewsPost { get; set; }
   public List<SelectListItem> NewsItem { get; set; }
   public List<SelectListItem> NewsGroup { get; set; }

   public NewsAdminViewData(List<SelectListItem> newsItem, List<SelectListItem> newsGroup, NewsPost newsPost)
   {
      this.NewsItem = newsItem;
      this.NewsGroup = newsGroup;
      this.NewsPost = newsPost;
   }
}

Here is my View:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MVCApp.Models.ViewModels.News.NewsAdminViewData>" %>

    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
        <% using (Html.BeginForm())
           {%>
        <div class="moduleContainer">
            <div class="moduleTitle">
                Create News Item
            </div>
            <div class="moduleContent">
                <div>
                    <div>
                        Title:</div>
                    <div>
                        <%= Html.TextBox("Title") %>
                    </div>
                </div>
                <div>
                    <div>
                         </div>
                    <div>
                        <%= Html.TextArea("Body") %>
                    </div>
                </div>
                <div>
                    <div>
                        News Group:
                    </div>
                    <div>
                        <%= Html.DropDownList("NewsGroup")%>
                    </div>
                </div>
                <div>
                    <div>
                        News Item:
                    </div>
                    <div>
                        <%= Html.DropDownList("NewsItem") %>
                    </div>
                </div>
            </div>
            <div class="moduleFooter">
                <%= Html.SubmitButton("btnSubmit", "Add News Post", null, "To add this news post click here.", "#ffd40f")%>
            </div>
        </div>
        <% } %>
        <%= Html.ClientSideValidation<NewsPost>()%>

And finally my post action:

 [AcceptVerbs(HttpVerbs.Post)]
    public virtual ActionResult Create(/*FormCollection collection*/ NewsPost np)
    {
       NewsPost entity = this.reposNewsPost.New();
       try
       {
          entity.Title = np.Title; 
          entity.NewsPostGUID = System.Guid.NewGuid();
          entity.DateAdded = DateTime.Now;
          entity.DateUpdated = DateTime.Now;
          entity.Body = np.Body; 

          UpdateModel(entity);
          this.reposNewsPost.Insert(entity);
          this.reposNewsPost.SubmitChanges();
          return RedirectToAction("Index");
        }
        catch (RulesException ex)
        {
           ex.AddModelStateErrors(ModelState, "NewsPost");
           return ModelState.IsValid ? RedirectToAction(MVC.News.Actions.Create) 
                        : (ActionResult)View();
        }
     }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对岸观火 2024-08-14 21:12:05

我遇到了和你类似的问题。老实说,看起来你不像我那样把事情弄得一团糟,但也许它会引导你找到解决方案。

我的带有 xVal 客户端验证的输入表单被动态加载(jquery ajax 调用)到页面中。不幸的是,我忽略了我将返回的 html 放入父元素之间已经具有“form”的元素中。因为它是一个ajax请求,所以页面上存在嵌套的“form”元素,并且xVal方法“_attachRuleToDOMElement”是这样实现的:

 var parentForm = element.parents("form");
        if (parentForm.length != 1)// <-- there can be only one parent form of course
            alert("Error: Element " + element.attr("id") + " is not in a form");

我的parentForm.lengts是2!

我发现您在这里肯定没有嵌套表单,但也许在母版页中有一些东西?可能是 IE7 呈现嵌套表单,而 Firefox 不呈现。
另外,我会对输入控件进行不同的命名,尽管这不应该是问题的根源:

<%= Html.TextBox("np.Title") %>...<%= Html.TextBox("np.Body") %> etc...

然后我也会设置 elementPrefix:

<%= Html.ClientSideValidation<NewsPost>("np")%>

I ran into the similar problem as you have. Honestly, it does not look like you made the mess as I have, but maybe it will direct you to the solution.

Mine input form with xVal client validation was loaded dynamically (jquery ajax call) into the page. Unfortunately, I overlooked that I was placing the returned html into the element that already has 'form' between parents elements. Because it was an ajax request, nested 'form' elements where present on the page, and xVal method "_attachRuleToDOMElement" is implemented this way:

 var parentForm = element.parents("form");
        if (parentForm.length != 1)// <-- there can be only one parent form of course
            alert("Error: Element " + element.attr("id") + " is not in a form");

Mine parentForm.lengts was 2!

I see that you don't have nested form here for sure, but maybe in master page there is something? Could be that IE7 renders nested forms and Firefox does not.
Also, I would name the input controls differently, although this should not be the source of the problem:

<%= Html.TextBox("np.Title") %>...<%= Html.TextBox("np.Body") %> etc...

and then I would set elementPrefix too:

<%= Html.ClientSideValidation<NewsPost>("np")%>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文