开发者

How to improve the solution

This solution works, but I think it can much better be done

JQuery:

$('#addMessage').click(function () {
    var textMessage = $('#ticketMessage').val();
    var isInternal = $('#isInternal')[0].checked;
    var ticketID = $('#TicketID').val();
    $.ajax({
        url: '/Ticket/AddMessage',
        type: 'POST',
        data: { textMessage: textMessage, isInternal: isInternal, ticketID: ticketID },
        success: function (data) {
            var tbody = $('#allMessages').children()[0];
            tbody.innerHTML = tbody.innerHTML + data;
            $('#ticketMessage').val("");
            $('#isInternal')[0].checked = false;
        }
    });
});

controller

  public string AddMessage(string textMessage, bool isInternal, int ticketID)
        {


     Message message = new Message();
        message.IsInternal = isInternal;
        message.TicketMessage = textMessage;
        message.TicketID = ticketID;
        DateTime created=DateTime.Now;
        message开发者_如何学编程.CreatedDateTime = created;
        message.PersonID = AppSecurity.Security.GetPersonID(Session);

        var personRepository =  new PersonRepository(_context);
        MessageRepository messageRepository = new MessageRepository(_context);
        messageRepository.Add(message);
        _context.SaveChanges();

        string relSrc = (personRepository.GetById((int)message.PersonID) as Employee).Image;
        string source = "";
        string isInternalStr = "";
        if (message.IsInternal) 
            isInternalStr = "Internal";
        if (message.Person is Employee) { source = relSrc != null ? "../../Images/TicketFiles" + relSrc.Replace('\\', '/') : "../../Images/TicketFiles/Employees/no-profile.png"; }
        String response = "<tr><td style=\"width: 25%\" valign=\"top\"><table><tr>" 
            + "<td><img src=\""+source+"\" alt=\"\" style=\"height: 60px\"/></td>" 
            + "</tr><tr><td>" 
            + AppSecurity.Security.GetUserFullName(Session)
            + "</td></tr><tr><td>"
            + created.ToString("dd.MM.yyyy") + " - " + created.ToString("HH:mm:ss")
            + "</td></tr></table></td><td style=\"width: 75%; padding:0px;\" valign=\"top\"><table style=\"width: 100%; height: 130px\" cellspacing=\"0\" cellpadding=\"0\">"
            + "<tr><td style=\"height: 20px; padding: 0px\">" + isInternalStr + "</td></tr><tr><td valign=\"top\">" + message.TicketMessage + "</td><tr></table></td></tr>";

        return response;
    }


Instead of generating the markup in the controller why not do it client side in the JavaScript? Your controller should not be concerned with markup


It seems to me you can benefit a lot by using jquery templates


Your controller should be of type ActionResult and as mr.nicksta said, should be made in a view.

public ActionResult AddMessage(string textMessage, bool isInternal, int ticketID)
{
    ...
    return View(message);
}

and then create a view with the same as the controller, strongly-typed with Message.

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<Message>" %>

Where you create the display. You could even do this as a partial view to it can be included in other pages more easily.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜