在我的 MVC 5 视图中看不到我的控制器对象

本文关键字:我的 看不到 控制器 对象 视图 MVC | 更新日期: 2024-10-31 09:43:58

我是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 代码中使用数据吗?

谢谢

在我的 MVC 5 视图中看不到我的控制器对象

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

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

编辑:我看到了你的问题,关于如何使用jQuery访问该信息。如果您按照前面的示例进行操作,则只需通过

$("#opinion").val()