Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.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
Jquery 客户端验证不适用于MVC4_Jquery_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

Jquery 客户端验证不适用于MVC4

Jquery 客户端验证不适用于MVC4,jquery,asp.net-mvc,asp.net-mvc-4,Jquery,Asp.net Mvc,Asp.net Mvc 4,出于某种原因,我在mvc 3中正确地验证了相同的代码,但在mvc 4中从未调用客户端验证。本质上,我需要客户端验证在jquery模式对话框中工作。有没有想过为什么不调用客户端验证 Web.config <appSettings> <add key="webpages:Version" value="2.0.0.0" /> <add key="webpages:Enabled" value="false" /> <add key="Pre

出于某种原因,我在mvc 3中正确地验证了相同的代码,但在mvc 4中从未调用客户端验证。本质上,我需要客户端验证在jquery模式对话框中工作。有没有想过为什么不调用客户端验证

Web.config

<appSettings>
   <add key="webpages:Version" value="2.0.0.0" />
   <add key="webpages:Enabled" value="false" />
   <add key="PreserveLoginUrl" value="true" />
   <add key="ClientValidationEnabled" value="true" />
   <add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
示例演示如何将数据发布到控制器方法

剃须刀代码:

<div id="form">
        <table width="600">
            <tr>
                <td>Select Date:</td>
                <td>
                    <input class="txtDate" type="date" size="20"></td>
            </tr>
            <tr>
                <td>Select Expense Type:</td>
                <td>
                    <select class="ExpenseType">
                        <optgroup label="Room">
                            <option>Room Fare</option>
                        </optgroup>

                        <optgroup label="Mess">
                            <option>Monthly Mess</option>
                        </optgroup>

                        <optgroup label="Others">
                            <option>Bus Fare</option>
                            <option>Tapari</option>
                            <option>Mobile Recharge</option>
                            <option>Auto</option>
                        </optgroup>
                    </select></td>
            </tr>
            <tr>
                <td>Enter Cost:</td>
                <td>
                    <input  class="cost" type="text" size="45" /></td>
            </tr>
            <tr>
                <td>Extra Details:</td>
                <td>
                    <input class="extra" type="text" size="45" /></td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td>
                    <button href="javascript:void(0);" onClick="saveExpense();" >Submit</button></td>
            </tr>
        </table>
    </div>

<script>
function saveExpense()
    {
        var expenseobject = {
            date:$('.txtDate').val() ,
            type:$('.ExpenseType').val() ,
            cost: $('.cost').val(),
            extra:$('.extra').val()

        };

        $.ajax({
            url: './saveexpense',
            type: 'POST',
            contentType: 'application/json',
            data: JSON.stringify({ obj: expenseobject }),
            success: function (result) {
                handleData(result);

            }
        });

    }
</script>
 public ActionResult SaveExpense(Expense obj)
        {
            obj.ExpenseId = Guid.NewGuid();
            if (ModelState.IsValid)
            {
                context.expenses.Add(obj);
                context.SaveChanges();
                int total=context.expenses.Sum(x => x.cost);
                return Json(new {spent=total,status="Saved" });

            }
            else
                return Json(new { status="Error"});   
        }
费用是我的模型,其对象的字段被创建为obj。。。
希望您现在就可以完成…

您尚未在模型中放置任何验证消息。我已为所需的批注放置了错误消息。还是没有区别。我点击提交按钮,对话框关闭,即使成功返回false.try alert(结果);在关闭对话框查看它包含的内容之前…或者您可以在Firefox或chrome中使用Firebug来查看脚本有什么问题…我无法在此处测试它,因为我没有其他代码…Firebug通知我调用了post方法,但成功返回为false。检查上面的控制器代码意味着您没有正确创建模型的对象。你能给我看一下收集ReportModel的代码吗?
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Globalization;
using System.Web.Mvc;
using System.Web.Security;

namespace DefaultMvc4App.Models {
    public class ReportModel {
        [Key]
        public Guid ReportID { get; set; }

        [Required]
        public string Month { get; set; }
        public IEnumerable<SelectListItem> AllMonths {
            get { return new SelectList(Utils.GetMonths()); }
        }

        [Required]
        public string Year { get; set; }
        public IEnumerable<SelectListItem> AllYears {
            get { return new SelectList(Utils.GetYears()); }
        }

        [Required]
        public string GeneralMeeting { get; set; }
        public IEnumerable<SelectListItem> YesNo {
            get { return new SelectList(Utils.GetYesNo()); }
        }
    }
}
    [HttpPost]
    public ActionResult CreateReport(ReportModel report) {
        if (ModelState.IsValid) {
            try {
                report.ReportID = Guid.NewGuid();
                db.report.Add(report);
                db.SaveChanges();
                return Json(new { success = true });
            }
            catch (Exception e) {
                return Json(new { success = false });
            }
        }
        return Json(new { success = false });
    }
<div id="form">
        <table width="600">
            <tr>
                <td>Select Date:</td>
                <td>
                    <input class="txtDate" type="date" size="20"></td>
            </tr>
            <tr>
                <td>Select Expense Type:</td>
                <td>
                    <select class="ExpenseType">
                        <optgroup label="Room">
                            <option>Room Fare</option>
                        </optgroup>

                        <optgroup label="Mess">
                            <option>Monthly Mess</option>
                        </optgroup>

                        <optgroup label="Others">
                            <option>Bus Fare</option>
                            <option>Tapari</option>
                            <option>Mobile Recharge</option>
                            <option>Auto</option>
                        </optgroup>
                    </select></td>
            </tr>
            <tr>
                <td>Enter Cost:</td>
                <td>
                    <input  class="cost" type="text" size="45" /></td>
            </tr>
            <tr>
                <td>Extra Details:</td>
                <td>
                    <input class="extra" type="text" size="45" /></td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td>
                    <button href="javascript:void(0);" onClick="saveExpense();" >Submit</button></td>
            </tr>
        </table>
    </div>

<script>
function saveExpense()
    {
        var expenseobject = {
            date:$('.txtDate').val() ,
            type:$('.ExpenseType').val() ,
            cost: $('.cost').val(),
            extra:$('.extra').val()

        };

        $.ajax({
            url: './saveexpense',
            type: 'POST',
            contentType: 'application/json',
            data: JSON.stringify({ obj: expenseobject }),
            success: function (result) {
                handleData(result);

            }
        });

    }
</script>
 public ActionResult SaveExpense(Expense obj)
        {
            obj.ExpenseId = Guid.NewGuid();
            if (ModelState.IsValid)
            {
                context.expenses.Add(obj);
                context.SaveChanges();
                int total=context.expenses.Sum(x => x.cost);
                return Json(new {spent=total,status="Saved" });

            }
            else
                return Json(new { status="Error"});   
        }