Scott
Scott

Reputation: 2760

Calling the same controller method again does not work

I have the following controller method in my WriteupController.cs:

public async Task<ActionResult> Writeups(string symbol, string status,  
    DateTime? mktDate, DateTime? writeupDate)
{
    // get datamodel
    return View(datamodel);
}

This is called using

<a href="@Url.Action("Writeups", "Writeup", new { symbol = item.SecSymbol, status = item.Status, mktDate = "10/10/2010", writeupDate = "10/11/2010"  })

and the proper view cshtml is displayed. So far, so good. There is dropdown list on the page with symbols. When one is selected, I have the following ajax call:

            $("#companiesDDL").change(function () {   
            var postdata = { "symbol": "1099.HK", "status": "A", "mktDate": "10/10/2010", "writeupDate": "10/10/2010" };
            $.ajax({
                url: '@Url.Action("Writeups", "Writeup")',
                dataType: "json",
                data: postdata,
                success: function (result, status, xhr) {
                    alert("Result: " + status + " " + xhr.status + " " + xhr.statusText)                   
                },
                error: function (xhr, status, error) {                     
                }
            });
        });

As you can see, the url is the same. When debugging, I can see the correct data in my datamodel in the controller method, and also in the cshtml view itself. However, the page is not loaded. That is, I DO NOT see the information for the new symbol. How do I fix this problem?

EDIT:

The following is from my view. As stated, originally I see the CompDescShort. But not when I call from the ajax.

            <textarea name="editor" id="editor" rows="10" cols="80">
                @if (Model.Count > 0)
                {
                        @Model.CompDescShort
                }
            </textarea>

FIXED, BY USING:

        <select id="companiesDDL" class="tmpdisplay" style="margin-left: 15px;" onchange="location.href=this.value">
            @foreach (var item in Model.companyList)
            {
                <option value='@Url.Action("Writeups", "Writeup", new { symbol = item.SecSymbol, status = "A" })'>@item.SecDesc</option>
            }
        </select>

Upvotes: 1

Views: 87

Answers (1)

DiskJunky
DiskJunky

Reputation: 4971

I appreciate that you have a fix that does a full page refresh when an item is selected but taking your original ajax approach, the following should work;

$("#companiesDDL").change(function () {   
        var postdata = { "symbol": "1099.HK", "status": "A", "mktDate": "10/10/2010", "writeupDate": "10/10/2010" };
        $.ajax({
            url: '@Url.Action("Writeups", "Writeup")',
            dataType: "json",
            data: postdata,
            success: function (result, status, xhr) {
                $('#editor).val(xhr.statusText);
            },
            error: function (xhr, status, error) {                     
            }
        });

The bit you were missing was that when the ajax call completes, it's up to the caller to decide what to do with it. In this case, we locate the <textarea> element in jQuery using the ID locator $('#editor') and set its value with val().

Upvotes: 1

Related Questions