Yuanye
Yuanye

Reputation: 3

Java Script passing value using MVC4

My Create view was like this:

@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.
}

here is the code of my 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) {
                    }
                })
            }
        })    

basically I want to implement my function with this JS to display not only of title related to the project but also all the activities has the same project name.

That's why I'm writing "success: function (@ViewBag.Activities) in my jquery call back function."

here is the method in my controller:

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");
    }

but when I am using breakpoint to debug, there are @ViewBag.Activities returned with value and counts, but seems like didn't display on my screen, anyone has any thoughts on that? any contribute will be greatly appreciated.

Update:

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

Upvotes: 0

Views: 94

Answers (1)

Shyju
Shyju

Reputation: 218722

If you set some values in ViewBag with an ajax request, you won't get it. The rite way to handle this is, simply return the data in JSON format and access it.

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);
}

The above will return a JSON representation of an anonymous object like this

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

And in your ajax method's success callback, access it like

success(function(data){
   // now you can acceess data.Projects and data.Activities
}

Upvotes: 1

Related Questions