首页 新闻 搜索 专区 学院

Ajax.BeginForm 表单提交之后,验证提示会丢失是啥原因?

0
悬赏园豆:30 [已关闭问题] 关闭于 2014-02-12 09:26

@model  test.Models.KeyWordViewModel

<script src="~/Scripts/jquery-1.7.1.js"></script>
<script src="~/Scripts/jquery.validate.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
<!--新增新闻类型-->
<div class="pop-box">
    <p></p>
    <fieldset>
        @using (Ajax.BeginForm("Index", "AddText", FormMethod.Post, new AjaxOptions { OnSuccess = "SuccessFunction" },       
            new { id = "pop-form" }))
        {
            <ul>
                   <li class="mb8">
                    <strong>关键词:</strong>
                    <input id="KeyName" value="@Model.KeyName"
                    type="text" size="40" class="ipt" data-val-required="" data-val="true"  name="KeyName"/>
                    <span class="star">*</span>
                    @Html.ValidationMessage("KeyName")
                    @*<span class="firld-validation-valid" data-valmsg-replace="true"
                        data-valmsg-for="KeyName"></span>*@
                </li>
                <li class="mb8 mt10">
                    <strong>显示排序:</strong><input id="Sort" name="Sort" value="@Model.Sort"
                      class="ipt" data-val-required="" data-val="true"  name="Sort"
                           type="text" size="2" />
                    <span class="star">*</span><span style="color: red;"> @Html.ValidationMessage("Sort") 数字越大优先级越高</span>
                </li>
                <input type="submit" value="登录" id="submit" />
                @*    <li class="last"><a id="btnaddsubmit" class="btn-red" onclick="add()" href="#">确定</a><a class="btn-gray" href="#">取消</a></li>*@
            </ul>
        }

    </fieldset>
</div>
<style>
    .pop-box {
        background-color: #fff;
    }
</style>

<script src="~/Scripts/iframeTools.source.js"></script>
<script type="text/javascript">
    $(document).ready(function () {

    });

    function add() {
        $("#pop-form").summbit();
    }


    function SuccessFunction() {
        //debugger
        //alert(1);
    }

</script>


----------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace test.Models
{
    /// <summary>
    /// 关键词管理
    /// </summary>
    public class KeyWordViewModel
    {
        #region Model       
        /// <summary>
        /// 显示名称
        /// </summary>
        [Required(ErrorMessage = "必填")]     
        [StringLength(100, ErrorMessage = "输入2到100的字符", MinimumLength = 2)]
        public string KeyName { set; get; }
      
        /// <summary>
        /// 显示排序
        /// </summary>
        [Required(ErrorMessage="必填")]
        [Range(0, 100000, ErrorMessage = "0-100000")]
        public int? Sort { set; get; }       

        #endregion Model
    }

}

-------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using test.Models;
namespace test.Controllers
{
    public class AddTextController : Controller
    {

        public ActionResult Add()
        {
            KeyWordViewModel model = new KeyWordViewModel();
            ModelState.AddModelError("error", "提供的用户名或密码不正确。");
            return View("Add",model);
        }

        [HttpPost]
        public ActionResult Index(KeyWordViewModel model)
        {
            //没验证成功
            if (!ModelState.IsValid)
            {
                //ModelState.AddModelError("KeyName", "服务器端的方法返回的。");
                return View("Add", model);
            }
                //验证成功
            else {
              
            }

            return View("Add", model);
        }


    }
}
 操作:当输入不合法的数据,然后点击提交,返回来的数据,没有添加样式提示,啥原因?

红萝卜的主页 红萝卜 | 菜鸟二级 | 园豆:240
提问于:2013-12-16 16:26
< >
分享
所有回答(1)
0

请问这个问题解决了吗?我也遇到这个问题。我原来用的 @using(Html.BeginForm()),换成@using (Ajax.BeginForm())服务器端验证返回的提示就出不来了。

xhma44 | 园豆:171 (初学一级) | 2015-12-10 15:13
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册