将RadioButtonFor与enum一起使用会使模型无法验证

时间:2015-03-10 20:03:26

标签: c# model-view-controller enums

将[{1}}与$("#formtest").valid()一起使用时,false总是enum,我遇到了问题。 如果我删除RadioButtonFor RadioButtonFor现在是$("#formtest").valid()

有人可以建议如何解决这个问题吗?

我的设置:

这是我的代码:

TestSurfaceController.cs

Test.cshtml:

<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使用

TestViewModel.cs:

@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>
}

TestDataModel.cs

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; }
    }

}

NotificationsFrequency.cs

namespace MyProject_Umbraco.Models
{
    public class TestDataModel
    {
        public string FullName { get; set; }
        public NotificationsFrequency NotificationsFrequency { get; set; }
    }
}

呈现的HTML:

public enum NotificationsFrequency
{
    Instantly = 1,
    Daily = 2
}

1 个答案:

答案 0 :(得分:0)

管理来解决这个问题。

事实证明,存在一些冲突的javascript库。通过将库更新为最新版本来修复。