0

そのビュー自体にある JScript に変数を渡したい MVC アプリケーションを開発しています。どうやってするか?

AccessMode の値を Javascript に送信したい。
その変数を宣言しましたが、JScript で使用する方法がわかりません。

<script src="@Url.Content("~/Scripts/jquery-1.8.0.min.js")" type="text/javascript"></script>

@model CRMEntities.Settings




@{
    ViewBag.Title = "CustomSettings3";
    //Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>CustomSettings3</h2>
<!DOCTYPE html>

<html>
<head>
    <title>CustomSettings3</title>
</head>
<body>

       @{string AccessMode; }
     @{string Ratings; }


       <div class="editor-label">
            @Html.LabelFor(model => model.AccessMode)
        </div>
        <div class="editor-field">
           @* @Html.EditorFor(model => model.AccessMode)*@
            @Html.DropDownList("AccessMode")
            @Html.ValidationMessageFor(model => model.AccessMode)
             //Assigning value.
            AccessMode = model.AccessMode
        </div>

         <div class="editor-field">

         @Html.DropDownList("RatingsCount")
        @Html.ValidationMessageFor(model => model.RatingsCount)
         @{
             Ratings = Html.DropDownList("RatingsCount");
        }
    </div>


        <p>
            <input type="button" value="Save" class="SaveSettings"/>       

        </p>

</body>
</html>




  <script type="text/javascript">

$(document).ready( function(){

    $('.SaveSettings').click(function ()
    {


           $.ajax
            ({

            type: 'post',
            url: '@Url.Content("~/Settings/SaveBasicSettings")',
            dataType: 'json',
            data:
            { 'AccessMode': "@AccessMode" },
            success: function (data) 
            {


            }

            });


       });

  });

</script>

ここに画像の説明を入力

4

2 に答える 2

3

Given that AccessMode is just a string variable defined in your view, and your JS is embedded in the same view, you can simple write the value of the variable to your JS in the standard way:

{ 'AccessMode': "@AccessMode" },

Another way you can do this once you've moved your JS to a seperate file (if you choose to do so) would be to store the AccessModel value as a data atribute on the editor field.

That way your javascript can simply pick it up as it would any other attribute on a page.

Update:

The way you're assigning the value needs adjusting also:

@{
AccessMode = Model.AccessMode
}
于 2012-10-19T12:15:17.293 に答える
0

値が適切に設定されている場合は、それを隠れた変数に保存してjavascriptで読み取ることができます

ビューで

<div class="editor-label">
            @Html.LabelFor(model => model.AccessMode)
        </div>
        <div class="editor-field">
           @* @Html.EditorFor(model => model.AccessMode)*@
            @Html.DropDownList("AccessMode")
            @Html.ValidationMessageFor(model => model.AccessMode)
             //Assigning value.
            AccessMode = model.AccessMode

           @Html.Hidden("modelValue", AccessMode)  //set value in hidden variable 
        </div>

javaScriptで

<script type="text/javascript">
    $(document).ready(function () {

        $('.SaveSettings').click(function () {
            var modelValue = $("#modelValue").val();
            $.ajax
            ({

                type: 'post',
                url: '@Url.Content("~/Settings/SaveBasicSettings")',
                dataType: 'json',
                data: { 'AccessMode': modelValue },
                success: function (data) {


                }

            });


        });

    });

</script>

注:値がmodel.AccessModeのHidden変数に正しく設定されているかどうかを確認してください。

于 2012-10-19T12:55:31.863 に答える