another approach to returning some thing to browse

2019-08-27 02:09发布

问题:

i have one section in my mvc 2.0 project which doing some processes and after each, return some messages (string) with response.write(). and this messages returned to browser with bad format. i want to return messages to one specific HTML div and add each to end of contents of div tag. now how do this?

this event after each procces raised and message returned to browser.

public void OnProgressEvent(System.Object source, CustomEventArgs customEventArgs)
{
    if (customEventArgs.Level > 5)
    {
        Response.Write(customEventArgs.Message + "<br />");
        Response.Flush();
    }
}

回答1:

jQuery is your friend here. If you make an ajax call to say an ActionResult, then you can either return a json object or a partial view.

My preference is to return a partial view and then replace the contents of the div with the resultant html.

So;

public ActionResult jQueryTagDelete(string SomeParametersMaybe)
{
    return PartialView("TagList", tags.OrderBy(x => x.keyword1));
}

And you jQuery code;

    function deleteTag(tagName) {
        $.post("/Admin/jQueryTagDelete", { tag: tagName }, function(RETURNED_HTML) {
            document.getElementById("divTags").innerHTML = RETURNED_HTML;
        });
    }