使用MVC 4传递值的Javascript

时间:2014-02-28 21:41:14

标签: javascript jquery asp.net-mvc

我的创建视图是这样的:

@model ContactProject.Models.Activity.Activity
@{
ViewBag.Title = "Create";
IEnumerable<ContactProject.Models.Activity.Activity> activities = ViewBag.Activities;
}
    <div class="editor-label">
        @Html.Label("Project")
    </div>
    <div class="editor-field">
       @Html.DropDownList("Projects", ViewBag.Project as SelectList,"----Project----", new { id = "ProjectsID" })
       @Html.ValidationMessageFor(model => model.iProjectID)
    </div>
    <div class="editor-label">
        @Html.Label("Title")
    </div>
    <div class="editor-field" id="TitlesDivID">
        <select id="TitlesID" name="Titles"></select>
    </div>
// other codes...
if (activities !=null){
// Display activities.
}

这是我的JS的代码

$('#ProjectsID').change(function () {
        var URL = 'TitleList';          
        $.getJSON(URL + '/' + $('#ProjectsID').val(), function (data) {
            var items = '<option>Select a Title</option>';
            $.each(data, function (i, title) {
                items += "<option value='" + title.Value + "'>" + title.Text + "</option>";
            if (items != null)
            {
                var addValue = $('#ProjectsID').val();             
                $.ajax
                ({ 
                    type: "POST",
                    url: "/Activity/getCreateActivities",
                    data: { projectID: addValue },
                    success: function (@ViewBag.Activities) {
                    }
                })
            }
        })    

基本上我想用这个JS实现我的功能,不仅要显示与项目相关的标题,还要显示所有活动都有相同的项目名称。

这就是为什么我在我的jquery回调函数中编写“success:function(@ ViewBag.Activities)”。

这是我控制器中的方法:

public ActionResult getCreateActivities(string projectID) 
    {
    int iProjectID = Int32.Parse(projectID);
    ViewBag.Project = GetProjects();
    var Activities = (from a in db.Activities
                     where a.iProjectID == iProjectID
                     select a).ToList();
    ViewBag.Activities = Activities;
    return View("Create");
    }

但是当我使用断点进行调试时,@ ViewBag.Activities返回值和计数,但似乎没有显示在我的屏幕上,任何人对此有什么想法?任何贡献将不胜感激。

更新

<table>
@foreach (var item in Activities) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.ProjectID)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Project.ProjectName)
    </td>
</tr>

1 个答案:

答案 0 :(得分:1)

如果您在ViewBag中使用ajax请求设置了一些值,则无法获得它。处理此问题的一种方式是,只需以 JSON 格式返回数据并访问它。

public ActionResult getCreateActivities(string projectID) 
{
    int iProjectID = Int32.Parse(projectID);        
    var Activities = (from a in db.Activities
                 where a.iProjectID == iProjectID
                 select a).ToList();

    return Json(new { Projects : GetProjects(),
                      Activities : Activities } ,JsonRequestBehaviour.AllowGet);
}

以上将返回匿名对象的JSON表示,如此

{
    "Projects": [ { "Id": 2 } ],
    "Activities": [ { "id": 113,"Name": "My name" } ]
}

在你的ajax方法的成功回调中,像

一样访问它
success(function(data){
   // now you can acceess data.Projects and data.Activities
}