在mvc4中验证后无法提交数据

时间:2013-02-07 13:03:02

标签: c# asp.net-mvc-4

大家好我已经完成了一个注册表单,它可以选择检查用户名是否在数据库中可用,为此我使用了远程验证,根据我的要求可以正常工作但是在填写所有值并尝试提交表单未提交的数据。这是我的cshtml代码

@model forumAPP.Models.Register
@{
    ViewBag.Title = "Register";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<script src="../../Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.validate.min.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>

<script type="text/jscript">
    //get file size
    function GetFileSize(fileid) {
    try {
        var fileSize = 0;
        //for IE
        if ($.browser.msie) {
            //before making an object of ActiveXObject, 
            //please make sure ActiveX is enabled in your IE browser
            var objFSO = new ActiveXObject("Scripting.FileSystemObject"); var filePath = $("#" + fileid)[0].value;
            var objFile = objFSO.getFile(filePath);
            var fileSize = objFile.size; //size in kb
            fileSize = fileSize / 1048576; //size in mb 
        }
        //for FF, Safari, Opeara and Others
        else {
            fileSize = $("#" + fileid)[0].files[0].size //size in kb
            fileSize = fileSize / 1048576; //size in mb 
        }

        // alert("Uploaded File Size is" + fileSize + "MB");
        return fileSize;
    }
    catch (e) {
        alert("Error is :" + e);
    }
}

//get file path from client system
function getNameFromPath(strFilepath) {

    var objRE = new RegExp(/([^\/\\]+)$/);
    var strName = objRE.exec(strFilepath);

    if (strName == null) {
        return null;
    }
    else {
        return strName[0];
    }

}

$("#btnSubmit").live("click", function () {

    if ($('#fileToUpload').val() == "") {
        $("#spanfile").html("Please upload file");
        return false;
    }
    else {
        return checkfile();
    }
});

function checkfile() {
    var file = getNameFromPath($("#fileToUpload").val());
    if (file != null) {
        var extension = file.substr((file.lastIndexOf('.') + 1));
        //  alert(extension);
        switch (extension) {
            case 'jpg':
            case 'png':
            case 'gif':
            case 'pdf':
                flag = true;
                break;
            default:
                flag = false;
        }
    }
    if (flag == false) {
        $("#spanfile").text("You can upload only jpg,png,gif,pdf extension file");
        return false;

    }
    else {
        var size = GetFileSize('fileToUpload');
        if (size > 3) {
            $("#spanfile").text("You can upload file up to 3 MB");
            return false;
        }
        else {
            $("#spanfile").text("");
        }
    }
}
$(function () {
    $("#fileToUpload").change(function () {
        checkfile();
    });

});

</script>


@using (Html.BeginForm("Register", "User", FormMethod.Post, new { enctype = "multipart/form-data" }))

{ 
        <div style="float: left;">
            <ol>
                <li>
                    @Html.LabelFor(m => m.Username)
                    @Html.TextBoxFor(m => m.Username, new { maxlength = 50 })
                    &nbsp;&nbsp;<span class="input_error">@Html.ValidationMessageFor(m => m.Username)</span>
                </li>
                <br />
                <li>
                    @Html.LabelFor(m => m.Displayname)
                    @Html.TextBoxFor(m => m.Displayname, new { maxlength = 50 })
                    &nbsp;&nbsp;<span class="input_error">@Html.ValidationMessageFor(m => m.Displayname)</span>
                </li>
                <br />
                <li>
                    @Html.LabelFor(m => m.Email)
                    @Html.TextBoxFor(m => m.Email, new { maxlength = 50 })
                    &nbsp;&nbsp;<span class="input_error">@Html.ValidationMessageFor(m => m.Email)</span>
                </li>
                <br />
                <li>
                    @Html.LabelFor(m => m.password)
                    @Html.TextBoxFor(m => m.password, new { maxlength = 50 })
                    &nbsp;&nbsp;<span class="input_error">@Html.ValidationMessageFor(m => m.password)</span>
                </li>
                <br />
                <li>
                    @Html.LabelFor(m => m.PasswordConfirm)
                    @Html.TextBoxFor(m => m.PasswordConfirm, new { maxlength = 50 })
                    &nbsp;&nbsp; <span class="input_error">
                        @Html.ValidationMessageFor(m => m.PasswordConfirm)</span> </li>
                <br />
                <li>
                    <input type="file" id="fileToUpload" name="file" />
                    <span class="input_error" id="spanfile"></span></li>
            </ol>
            @*<label id="UserNameAvailabilityLabel" style="float: right; margin-left: 155px;">
            </label>*@
        </div>
        <br />
        <div style="float: right; margin-right: 350px;">
            <input type="submit" id="btnSubmit" value="Create User" />
            &nbsp;
            @Html.ActionLink("Cancel", "Index", "Home")
        </div>
 }

我的控制器代码

[OutputCache(Location = OutputCacheLocation.None, NoStore = true)]
        public JsonResult IsUserNameAvailable(string UserName)
        {
            var usrAvailable = db.tblUsers.Where(p => p.UserName == UserName).Select(img => img.UserName).FirstOrDefault();

            if (usrAvailable == null)
            {
                return Json("<span style='color:Green;'>Username is available</span>", JsonRequestBehavior.AllowGet);
            }
            //string html = "<span style='color:Red;'> Username in use</span>";
            return Json("<span style='color:Red;'> Username in already in use</span>", JsonRequestBehavior.AllowGet);

        }

示例截屏

enter image description here

enter image description here

enter image description here

如果我从View删除了提交数据的脚本,但我无法验证用户是否存在,那么有人可以帮助我按照我的要求实现

这是控制器代码

    [HttpPost]
    public ActionResult Register(forumAPP.Models.Register user, HttpPostedFileBase file)
    {
        if (ModelState.IsValid)
        {
             // do work
        }
     }

0 个答案:

没有答案