Tag: asp.net mvc

MVC4通过Ajax.BeginForm传递模型

我试图在这里关注一些好的post以使其工作,但每次我点击ajax回发时,我的控制器中的断点显示一个具有空值的模型。 此页面显示仅供查看的模型值,但我已尝试将它们放在自己的表单中,并将它们包装在ajax表单中,似乎没有任何效果。 @model VendorProfileIntranet.Models.VendorProfile $(function () { $(“form”).submit(function () { if ($(this).valid()) { $.ajax({ url: this.action, type: this.method, data: $(this).serialize(), success: function (result) { $(“#message”).html(result); } }); } return false; }); }); @using (Ajax.BeginForm(“SelectVendor”, “Home”, new AjaxOptions { HttpMethod=”post”, InsertionMode=InsertionMode.Replace, UpdateTargetId=”message” })) { 视图非常长(仅显示用于查看的模型值),此处缩写。 Contact Information @Html.HiddenFor(model => model.ProfileID) @Html.LabelFor(model => model.Name) @Html.DisplayFor(model => model.Name) […]

在asp.net mvc中使用jQuery动态删除表行

我有一个表,我动态添加和删除行: @model AHBReports.Models.AdjustmentModel @using (Html.BeginForm()) { @Html.EditorFor(model => model.Adjustments) } EditorTemplate: @model AHBReports.Models.Adjustment @Html.HiddenFor(x => x.Id, new { @class = “iHidden” }) @Html.AutocompleteFor(model => model.BRANCH, “GetBranches”, “Report700H”) @Html.EditorFor(model => model.Amount) x 表操作脚本: function removeRow(selector) { if ($(‘#container tr’).length > 1) { $(selector).closest(‘tr’).remove(); } } $(document).ready(function () { $(“#btnAdd”).click(function (e) { var ind = $(“#container tr:last”).find(“input.iHidden”).val(); […]

在jQuery中获取多个Textboxes值 – MVC

我是MVC的新手,我正在尝试编辑一行并通过jQuery和AJAX将编辑后的数据发送到控制器方法,当我点击edit ,特定行变为文本框并save ActionLink而不是edit ,但是当我保存它时给我一个例外,因为null进入data jQuery / Ajax代码: $(document).ready(function () { function toggleEditability() { $(this).closest(‘tr’) .find(‘a.Edit, a.Save, .displayText, input[type=text]’) } $(‘a.Edit’).click(toggleEditability); $(‘a.Save’).click(function () { toggleEditability.call(this); var data = $(this).closest(‘tr’).serialize(); alert(data); //var url = $(this).attr(‘href’); var actionURL = ‘@Url.Action(“Edit”, “Holiday”)’; $.ajax({ url: actionURL, // Url to send request to data:data, // Send the data to the server […]

在IIS中发布后不会呈现BundleConfig

我在服务器的IIS 7中发布我的MVC网站。 当我检查页面源时,我看到我的笔记本电脑没有正确显示。 以下是在IIS服务器中发布后的页面源: Index – abc System 以下是我在笔记本电脑上测试时的页面源代码: Index – abc System 以下是BundleConfig.cs的代码: public class BundleConfig { // For more information on bundling, visit http://go.microsoft.com/fwlink/?LinkId=301862 public static void RegisterBundles(BundleCollection bundles) { bundles.Add(new ScriptBundle(“~/bundles/jquery”).Include( “~/Scripts/jquery-{version}.js”)); bundles.Add(new ScriptBundle(“~/bundles/jquery”).Include( “~/Scripts/jquery-1.10.2.min.js”)); bundles.Add(new ScriptBundle(“~/bundles/jqueryui”).Include( “~/Scripts/jquery-ui.js”)); bundles.Add(new ScriptBundle(“~/bundles/jqueryval”).Include( “~/Scripts/jquery.validate*”)); // Use the development version of Modernizr to develop with and […]

哪个Data Annotation属性创建此validation属性?

假设我们有这样的模型: public class TheViewModel { public string DateTime? Image_Date { get; set; } } 它被添加到Razor视图中,如下所示: Html.TextBoxFor(model => model.Image_Date) 然后在浏览器中呈现以下内容: 属性data-val-date是我感兴趣的。它显然是由MVC的“不引人注目的”jQueryvalidation集成注入的。 那么,哪些数据注释会覆盖HTML属性中的措辞? 例如, [Required(ErrorMessage=”This field is required!”)]将覆盖标准“字段{0}是必需的”。 信息。 尝试失败: [DataType(DataType.Date, ErrorMessage = “Must be a valid date.”)]似乎对客户端validation没有任何作用。 [DisplayName(“…”)]更改模式的通配符部分,但显然不会影响模式本身。

Page_ClientValidate未定义(ASP.NET MVC)

我有一个ASP.NET MVC表单,如下所示: @using (Html.BeginForm(null, null, FormMethod.Post, new { id = “ccform” })) { @Html.ValidationSummary(true, “Please correct the errors and try again.”) … } 我对此表单有一些特殊处理,如下所示: $(function () { $(‘#ccform’).submit(function (e) { e.preventDefault(); if (typeof (Page_ClientValidate) == ‘function’) { Page_ClientValidate(); } if (Page_IsValid) { … } }); }); 但我发现Page_ClientValidate 总是未定义的。 搜索网络时,我发现当validation组件不可用时可能就是这种情况。 但似乎我需要的一切都是必需的。 有人可以提供一些提示吗? 编辑 正如内森所说,我偏离轨道。 在我的Google搜索中,我最终查看了WebFormvalidation。 事实certificate,在MVC中,答案非常简单。 […]

对象不支持属性或方法’valid’

我的代码抛出以下错误: 对象不支持属性或方法’valid’。 这是因为当前表单没有validation。 那没问题。 我想要的是提前检查我是否可以将该方法应用于当前表单以避免恼人的JavaScript错误。 这就是我想要的。 例如if(form.isValidatable()) form.valid() 代码没有任何问题。 我只需要知道调用方法或属性是否会抛出exception。 form.submit(function () { if ($(this).valid()) onFormIsValid($(this)); else onFormIsInvalid($(this)); }); 更新:我是jquery的新手,谢谢你的帮助!!!!! 我想要做的是在提交之前和validation之后禁用所有输入。 我没有设法调用submitHandler,而且很难理解为什么。 这就是我正在做这个解决方案的原因。 看我的代码。 我怎样才能改善这个? 我想将它应用于我的解决方案中的所有forms…. $(document).ready(function () { var form = $(“form”); form.bind(‘invalid-form.validate’, function() { onFormIsInvalid($(this)); }); form.submit(function () { if ($(this).valid()) onFormIsValid($(this)); else onFormIsInvalid($(this)); }); }); function onFormIsValid(form) { $(“input”, form).prop(“readonly”, “readonly”); $(“input[type=submit]”, […]

使用AJAX加载部分视图不起作用

请原谅我,我是MVC和AJAX的新手。 目前我只是提交一个表单,我想使用表单中的数据使用ajax在局部视图中更新表。 我的_UserInfo局部视图如下所示: @model IEnumerable Firstname Lastname EmailAddress @foreach (var item in Model) { @Html.DisplayFor(modelItem => item.firstName) @Html.DisplayFor(modelItem => item.surname) @Html.DisplayFor(modelItem => item.emailAddress) @Html.DisplayFor(modelItem => item.SAMuserName) } 我的主要User视图如下所示: @model Dashboard.Models.User @Scripts.Render(“~/bundles/jqueryval”) @using (Ajax.BeginForm(“_UserInfo”, “User”, new AjaxOptions() { HttpMethod = “GET”, UpdateTargetId = “results”, InsertionMode = InsertionMode.Replace })) { //…My Form } 最后这是我的UserController : public PartialViewResult […]

JsonResult为Jquery .ajax返回null

我正在尝试将一个Jquery .ajaxpost发送到MVC操作。 这个Action然后应该返回一个JsonResult。 目前,这个JsonResult是“”,但我希望返回对象用Json填充。 有帮助吗? 谢谢! $.ajax({ type: ‘post’, dataType: ‘json’, url: url, contentType: “application/json; charset=utf-8”, traditional: true, data: JSON.stringify({ source: source }), success: function (data) { debugger; } }); public ActionResult PublishSource(string source) { return Json(new {data = “test”}); } 编辑:这是我目前的代码。 仍然在.ajax的成功方法中返回数据为空。 $.ajax({ type: ‘post’, cache: false, url: url, contentType: “application/json”, success: function (data) […]

如何序列化模型,将其传递给ajax请求中的MVC控制器

我正在尝试将我的页面模型传递给我的控制器进行处理。 处理完信息之后,我想更新id“savedText”的div,以显示“Billing Information saved successfully successfully”。 在我看来,我有这个 function testingFunction() { var obj = $(‘testForm’).serialize(); $.ajax({ url: ‘@Url.Action(“TestingFunction”, “BuildGiftCard”)’, dataType: ‘json’, success: function (result) { $(“#savedText”).html(result); }, contentType: ‘application/json; charset=utf-8’, data: JSON.stringify(obj) }); return false; } 我在我的控制器中有这个: [HttpPost] public JsonResult TestingFunction(PurchaseModel model) { return Json(“Billing information saved successfully.”); } 我究竟做错了什么? 当Chrome中的“检查元素”时,在网络选项卡中,它表示找不到我的控制器方法。 此外,对我来说,将整个模型从视图中获取到控制器的函数(本例中为TestingFunction)非常重要,这样我就可以获取表单信息并保存它。 我正在尝试.serialize()函数,但结果是obj =(空字符串)。