[英]Using RadioButtonFor with enum makes model not validate
I have a problem where $("#formtest").valid()
is always false
when using an enum
together with a RadioButtonFor
. 我有一个问题,当将
enum
与RadioButtonFor
一起使用时, $("#formtest").valid()
始终为false
。 If I remove the RadioButtonFor
the $("#formtest").valid()
is now true
. 如果我删除
RadioButtonFor
$("#formtest").valid()
现在为true
。
Can anybody suggest how to fix this? 有人可以建议如何解决此问题吗?
My setup: 我的设置:
TestSurfaceController.cs
) to handle the POST via jQuery ajax TestSurfaceController.cs
)通过jQuery ajax处理POST This is my code: 这是我的代码:
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="/scripts/jquery.validate.js"></script>
<script src="/scripts/jquery.validate.unobtrusive.js"></script>
Test.cshtml: Test.cshtml:
@using MyProject_Umbraco.Models
@inherits UmbracoViewPage<MyProject_Umbraco.Models.TestViewModel>
@{
Layout = "Layout.cshtml";
}
@using (Html.BeginForm(null, null, FormMethod.Post, new {id = "formtest"}))
{
@Html.TextBoxFor(m => m.Model.FullName)
<ul>
<li>@Html.RadioButtonFor(m => m.Model.NotificationsFrequency, NotificationsFrequency.Instantly) Instant</li>
<li>@Html.RadioButtonFor(m => m.Model.NotificationsFrequency, NotificationsFrequency.Daily) Daily</li>
</ul>
<a id="btnSignup">Sign up</a>
}
@section BottomScripts
{
<script>
$(function() {
$("#btnSignup").on("click", function() {
signup();
});
var signup = function() {
$("#formtest").validate();
if ($("#formtest").valid()) { // always false
var obj = $("#formtest").serializeArray();
$.ajax({
url: "@Url.Action("Index", "TestSurface")",
type: "POST",
data: obj,
success: function(result) {
alert(result);
}
});
} // end if valid()
}; // end signup()
});
</script>
}
TestViewModel.cs: used by Test.cshtml TestViewModel.cs:由Test.cshtml使用
using System.Globalization;
using Umbraco.Core.Models;
using Umbraco.Web.Models;
namespace MyProject_Umbraco.Models
{
public class TestViewModel : RenderModel
{
public TestViewModel(IPublishedContent content, CultureInfo culture)
: base(content, culture)
{
Model = new TestDataModel();
}
public TestViewModel(IPublishedContent content)
: base(content)
{
Model = new TestDataModel();
}
public TestDataModel Model { get; set; }
}
}
TestDataModel.cs TestDataModel.cs
namespace MyProject_Umbraco.Models
{
public class TestDataModel
{
public string FullName { get; set; }
public NotificationsFrequency NotificationsFrequency { get; set; }
}
}
NotificationsFrequency.cs NotificationsFrequency.cs
public enum NotificationsFrequency
{
Instantly = 1,
Daily = 2
}
Rendered html: 呈现的html:
<form action="" id="formtest" method="post">
<input id="Model_FullName" name="Model.FullName" type="text" value="" /> <ul>
<li><input checked="checked" data-val="true" data-val-required="The NotificationsFrequency field is required." id="Model_NotificationsFrequency" name="Model.NotificationsFrequency" type="radio" value="Instantly" /> Instant</li>
<li><input id="Model_NotificationsFrequency" name="Model.NotificationsFrequency" type="radio" value="Daily" /> Daily</li>
</ul>
<a id="btnSignup">Sign up</a>
</form>
Managed to fix this. 设法解决这个问题。
It turns out there were some conflicting javascript libraries. 原来有一些冲突的javascript库。 Fixed by updating the libraries to the newest versions.
通过将库更新到最新版本进行修复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.