【问题标题】:Can't see my Controller object in my MVC 5 View在我的 MVC 5 视图中看不到我的控制器对象
【发布时间】:2015-03-07 11:46:33
【问题描述】:

我是 MVC 的新手,我正在使用这项技术编写我的第一个应用程序。 我正在尝试将一个对象从控制器返回到我的视图,但我在视图中看不到它,听到的是我的控制器代码:

` 
   public ActionResult Create(int id)
    {
        AddAffiliatModel serverUsersModel = new AddAffiliatModel();
        ObjectResult<getServerUsers_Result> serverUsers = serverUsersModel.getSUsers(); 
        List<getServerUsers_Result> users = serverUsers.ToList();
        return View(users[0]);
    }` 

这是我的查看代码:

            @model MVC_5_Skeleton1.Models.getServerUsers_Result
        <!DOCTYPE html>

        <html>
        <head>
            <meta name="viewport" content="width=device-width" />
            <title>getUsers</title>
            <script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
            <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
            <script src="~/JS/AddAffiliate.js"></script>
            <style>
                .txtW {
                    max-width: 300px;
                }

                .txtAH {
                    height: 200PX;
                }
            </style>
            <script type="text/javascript">
                //**************************************************************************
                //** Jquery Ready function hooks the buttons click events                  *
                //**************************************************************************
                $(document).ready(function () {


                    $(document).on('click', 'button', function (e) {
                        buttonId = $(this).attr("id");
                        switch (buttonId) {
                            case "submit":
                                Validate();
                                clearFields();
                                break;
                            case "cancel":

                                break;

                            default:
                                break;
                        }

                    });
                });
                //**************************************************************************
                //** Validate and send                                                     *
                //**************************************************************************
                function SendToServer() {

                }
                //**************************************************************************
                //** Validate and send                                                     *
                //**************************************************************************
                function Validate() {
                    var errorMsg = "";
                    var postString = "";
                    var valid = 0;
                    var filter = /^([a-zA-Z0-9_\.\-])+\@@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

                    if ($('#fullName').val().length > 2) {
                        var name = $('#fullName').val();

                    }
                    else {
                        errorMsg = 'Please enter name';
                        valid = 1;
                    }
                    if ($('#email').val().length > 5) {

                        var email = $('#email').val();
                    }
                    else {
                        errorMsg += 'Please valid email' + '/n';
                        valid = 1;
                    }
                    if ($('#skype').val().length > 3) {
                        var name = $('#skype').val();

                    }
                    else {
                        errorMsg = 'Please Skype name' + '/n';
                        valid = 1;
                    }
                    if ($('#Countries-leagues').val().length > 5) {
                        var cl = $('#Countries-leagues').val()
                    }
                    else {
                        errorMsg = 'Please enter Coverage' + '/n';
                        valid = 1;
                    }
                    if ($('#opinion').val().length > 5) {
                        var opinion = $('#opinion').val()
                    }
                    else {
                        errorMsg = 'Please enter Coverage' + '/n';
                        valid = 1;
                    }
                    if (valid == 0) {
                        send();
                    }
                }
                //**************************************************************************
                //** Get desired length and string to chech                                *
                //**************************************************************************
                function Send() {
                    AffiliateData.pType = 'AddAffiliate';
                    AffiliateData.user[0].recomanderId = "1"
                    AffiliateData.user[0].userName = $('#fullName').val();
                    AffiliateData.user[0].email = $('#email').val();
                    AffiliateData.user[0].skype = $('#skype').val();
                    AffiliateData.user[0].covered = $('#Countries-leagues').val();
                    AffiliateData.user[0].opinion = $('#opinion').val();
                    if ($('#canConnect').is(":checked")) {
                        AffiliateData.user[0].canContact = "1";
                    }
                    else {
                        AffiliateData.user[0].canContact = "0";
                    }
                    if ($('#aware').is(":checked")) {
                        AffiliateData.user[0].aware = "1";
                    }
                    else {
                        AffiliateData.user[0].aware = "0";
                    }
                    $.ajax({
                        type: "POST",
                        processData: false,
                        url: "http://localhost/MVC%205%20Skeleton1/AddAffilate/Create/1",
                        // The key needs to match your method's input parameter (case-sensitive).
                        data: JSON.stringify({ json: AffiliateData }),
                        //    dataType: "json",
                        success: function (data) {
                            alert(data);
                        },
                        failure: function (errMsg) {
                            alert(errMsg);
                        }
                    });
                }
                //**************************************************************************
                //** Clean UI Fields                                                       *
                //**************************************************************************
                function clearFields() {

                    $('#fullName').val("");
                    $('#email').val("");
                    $('#skype').val("");
                    $('#email').val("");
                    $('#Countries-leagues').val("");
                    $('#opinion').val("");
                    $('input:checkbox').removeAttr('checked');
                }
                //*********************      end of script block       *********************
            </script>
        </head>
        <body>
            <h2>Create new Affilate</h2>

            <fieldset>

                <!-- Form Name -->
                <!-- Text input-->
                <div class="form-group">
                    <div class="col-md-12">
                        <label class="control-label" for="textinput">Candidate Name </label>
                    </div>
                    <div class="col-md-12">
                        <input id="fullName" name="textinput" type="text" placeholder="Candidate Name " class="form-control input-md txtW" required="">
                    </div>
                </div>
                <!-- Text input-->
                <div class="form-group">
                    <div class="col-md-12">
                        <label class="control-label" for="email">Email</label>
                    </div>
                    <div class="col-md-12">
                        <input id="email" name="email" type="email" placeholder="Email" class="form-control input-md txtW" required="">
                    </div>
                </div>
                <!-- Text input-->
                <div class="form-group">
                    <div class="col-md-12">
                        <label class="control-label" for="skype">Skype</label>
                    </div>
                    <div class="col-md-12">
                        <input id="skype" name="skype" type="text" placeholder="Skype" class="form-control input-md txtW" required="">

                    </div>
                </div>

                <!-- Textarea -->
                <div class="form-group">
                    <label class="col-md-12 control-label" for="Countries-leagues">Countries/leagues</label>
                    <div class="col-md-12">
                        <textarea class="form-control txtW txtAH" id="Countries-leagues" name="Countries-leagues" required=""></textarea>
                    </div>
                </div>

                <!-- Textarea -->
                <div class="form-group">
                    <label class="col-md-12 control-label" for="opinion">Way are you recommending Him/Her</label>
                    <div class="col-md-4">
                        <textarea class="form-control txtW txtAH" id="opinion" name="opinion" required=""></textarea>
                    </div>
                </div>

                <!-- Multiple Checkboxes -->
                <div class="form-group">
                    <label class="col-md-12 control-label" for="checkboxes"></label>
                    <div class="col-md-4">
                        <div class="checkbox">
                            <label for="checkboxes-0">
                                <input type="checkbox" name="checkboxes" id="canConnect" value="0">
                                Can we contact he/she?
                            </label>
                        </div>
                        <div class="checkbox">
                            <label for="checkboxes-1">
                                <input type="checkbox" name="checkboxes" id="aware" value="0">
                                Dose he/she knows will approach them?
                            </label>
                        </div>
                    </div>
                </div>

                <!-- Button (Double) -->
                <div class="form-group">
                    <label class="col-md-12 control-label" for="submit"></label>
                    <div class="col-md-8">
                        <button id="submit" name="submit" class="btn btn-success">Submit</button>
                        <button id="cancel" name="cancel" class="btn btn-primary">Cancel</button>
                    </div>
                </div>

            </fieldset>
        </body>
        </html>

如何访问我的 @model ? 我可以在我的 Jquery 代码中使用这些数据吗?

谢谢

【问题讨论】:

  • 如果您使用的是 ASP.NET MVC,那么使用 HTML Helpers 是一个很好的做法。像 @Hml.TextBoxFor(x=>x.YourPropertyName) 一样,你会发现它在使用 Razor 视图引擎时很方便。
  • 在继续之前,我强烈建议您访问 MVC 站点并完成教程
  • Stephen 是对的,与其深入研究,您绝对应该阅读一些使 ASP.NET MVC 成为什么样的东西。 Pluralsight 有一个很棒的包罗万象的初学者教程。
  • 我做了阅读和一些教程,我的主要问题是使用 Jquery 使用数据
  • @ShimonWiener,您可以通过在属性上使用验证属性并使用包括@Html.ValidationMessageFor() 在内的强类型帮助器来获得客户端和服务器端验证,至少可以将脆弱代码减少 2/3。您的 Validate() 函数不是必需的,如果您想使用 ajax 发布表单,只需 $.post('@Url.Action("Create", "AddAffilate")', $('form').serialize(), function(data) { // do something with the return value });

标签: c# jquery asp.net-mvc model-view-controller


【解决方案1】:

您可以使用@Model.PropertyName 访问您的模型属性。 示例:

 <textarea class="form-control txtW txtAH" id="opinion" name="opinion" required="">@Model.opinion</textarea>

已编辑:我在您的问题上看到了有关如何使用 jQuery 访问该信息的评论。 如果你按照前面的例子,你可以简单地访问它

$("#opinion").val()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-05
    • 2016-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多