0

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>

1 Answer 1

1

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
}
Sign up to request clarification or add additional context in comments.

9 Comments

my mistake, I wanna return was kind of IEnumerable<activity> object, and the project was one properties of one activity.
But this is the idea, You return your data in JSON format.
then can just append this data.Activities directly to the table? like $("#ActivitiesTable").empty().append(data.Activities);
@user3366622 : Check this jsfiddle which updates the table with data. Use the $.each loop in your success event. jsfiddle.net/mnfZ8/2
If you want to take the results and just shove them into the grid, you can return a partial view from your controller and do some client-side replacement. If you are doing this a lot and would rather return JSON, you might want to look at a template engine (e.g., Handlebars) to construct HTML from your JSON result.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.