Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
965 views
in Technique[技术] by (71.8m points)

asp.net mvc 4 - Assigning ViewBag property in javascript

I am trying to assign a JS variable to a ViewBag property and use that property in an Html ActionLink.

However, I'm getting a design time compile error: "Syntax error" right below the getJSON method as @ViewBag.CustomerID = data.CustomerID;

Can someone help me out with how this is done?

Here is my code:

@Html.ActionLink("Click to edit Customer", "EditCust", "Customer", new { ViewBag.CustomerID }, null);

<script type="text/javascript">

        $(function ()
        {
            var selID = null
            $("#Name").change(function ()
            {
                selID = $("#Name option:selected").val();

                var url = '/Project/SpecificCustomer';
                var param = { Id: selID };
                $.getJSON(url, param, function (data)
                {
                    @ViewBag.CustomerID = data.CustomerID;
                    var html = "<table border='1' cellpadding='3'>";
                    html += "<tr>";
                    html += "<td>" + "Customer ID: " + data.CustomerID + "</td>";
                    html += "</tr>";
                    html += "<tr>";
                    html += "<td>" + "Email: " + data.Email + "</td>";
                    html += "</tr>";
                    html += "<tr>";
                    var FirstName = data.FirstName;
                    FirstName == null ? "" : FirstName;
                    var LastName = data.LastName;
                    LastName == null ? "" : LastName;
                    html += "<td>" + "Name: " + FirstName + " " + LastName + "</td>";
                    html += "</tr>";
                    html += "<tr>";
                    var date1 = new Date(parseInt(data.CreatedDate.substr(6)));
                    date1 == null ? "" : date1;
                    html += "<td>" + "Created: " + date1 + "</td>";
                    html += "</tr>";
                    html += "<tr>";
                    var date2 = new Date(parseInt(data.UpdatedDate.substr(6)));
                    date2 == null ? "" : date2;
                    html += "<td>" + "Updated: " + date2 + "</td>";
                    html += "</tr>";
                    html += "</table>";
                    $("#divData").html('');
                    $("#divData").append(html);
                });
            });
        });

    </script>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This won't work. ViewBag works on server side and it will be out of scope when you get the response of an AJAX request. You should use JavaScript to update the values in your view once you get the response.

You also get the error because an assignment should be in a code block:

@{ ViewBag.CustomerID = data.CustomerID; }

However this still won't work because data is a JavaScript variable.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...