Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am working with an MVC application. I have to use two post methods in my controller. one post method needs to call from the view and other from the javascript file.
I am writing my code here.
In Controller
C#
public class ChatRegisterController : Controller
    {
        public ActionResult Index()
        {
            Chat register = new Chat();
            return View(register);
        }

        SMWebDefaultConnection objSMWebDefaultConnection = new SMWebDefaultConnection();
        [HttpPost]
        public ActionResult Index(Chat register)
        {
            if (string.IsNullOrEmpty(register.conversationID))
            {
                register.conversationID = CreateConversationID();
                spUNCandidateChatRegister_Result chatRegResult = objSMWebDefaultConnection.smConn.spUNCandidateChatRegister(register.unCandidateFirstName, register.unCandMobilePhone, register.unCandEmail, register.enquiryTypeID, register.enquiryLabelID, register.conversationID).FirstOrDefault();
                objSMWebDefaultConnection.smConn.SaveChanges();
                register.candidateID = chatRegResult.unCandidateID.getInteger();
            }
            else
            {
                register.chatConversations = objSMWebDefaultConnection.smConn.tblChatConversations.Where(c => c.conversationID == register.conversationID).ToList();
            }
            ModelState.Clear();
            return View(register);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult SubmitChat(string unCandidateID, string conversationID, string conversationText)
        {
            objSMWebDefaultConnection.smConn.spChatConversation(conversationID, unCandidateID.getInteger(), conversationText);
            objSMWebDefaultConnection.smConn.SaveChanges();
            Chat chat = new Chat();
            chat.candidateID = unCandidateID.getInteger();
            chat.conversationID = conversationID;
            chat.unCandidateFirstName = objSMWebDefaultConnection.smConn.tblChats.Where(c => c.conversationID == conversationID).FirstOrDefault().candidateName;
            chat.chatConversations = objSMWebDefaultConnection.smConn.tblChatConversations.Where(c => c.conversationID == conversationID).ToList();
            ModelState.Clear();
            return Json("OK");
        }
}

In Javascript
C#
$(function () {

    $('#txtChatConversation').keypress(function (e) {
        var key = e.which || e.keyCode;
        if (key == 13) {
     
            $.ajax({
                type: "POST",
                url: "/ChatRegister/SubmitChat",
                data: "{'unCandidateID':" + $('#txtUNCandidateID').val() + ",'conversationID':'" + $('#txtConversationID').val() + "','conversationText':'" + $('#txtChatConversation').val() + "'}",               
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    window.location.reload(true);
                },
                error: function () {
                    alert("Failed to submit chat. Please try again.");
                    // window.location.reload(true);
                }
            });
        }
    });
});


My View
C#
@model SM_WEB.Models.Chat
@using SM_WEB.Models;
@{
    ViewBag.Title = "Santa Monica - Chat Register";
    Layout = "~/Views/Shared/_Layout.cshtml";
    if (!string.IsNullOrEmpty(Model.conversationID))
    {
        SMWebDefaultConnection objSMWebDefaultConnection = new SMWebDefaultConnection();
        Model.chatConversations = objSMWebDefaultConnection.smConn.tblChatConversations.Where(c => c.conversationID == Model.conversationID).ToList();
    }
}
<script src="../../Scripts/Page/chat.js" type="text/javascript"></script>
<div class="jumbotron ">
    <div class="container " style="padding: 130px 0 0 0;">
        @if (string.IsNullOrEmpty(Model.conversationID))
        {
            using (Html.BeginForm())
            {
                Html.ValidationSummary(true);
            <div class="row">
                <div class="col-lg-4 panel panel-default col-centered">
                    <div class="form-horizontal  top-buffer " role="form">
                        <div class="form-group">
                            <div style="display: none">
                                @Html.TextBoxFor(model => model.conversationID, new { @id = "txtConversationID" });
                            </div>
                            <label for="inputEmail3" class="col-sm-2 control-label">
                                Name</label>
                            <div class="col-sm-8">
                                @Html.TextBoxFor(model => model.unCandidateFirstName, new { @class = "form-control", placeholder = "Name" })
                                @Html.ValidationMessageFor(model => model.unCandidateFirstName)
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="inputEmail3" class="col-sm-2 control-label">
                                Phone</label>
                            <div class="col-sm-8">
                                @Html.TextBoxFor(model => model.unCandMobilePhone, new { @class = "form-control", placeholder = "Mobile No" })
                                @Html.ValidationMessageFor(model => model.unCandMobilePhone)
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="inputPassword3" class="col-sm-2 control-label">
                                Email</label>
                            <div class="col-sm-8">
                                @Html.TextBoxFor(model => model.unCandEmail, new { @class = "form-control", placeholder = "Email" })
                                @Html.ValidationMessageFor(model => model.unCandEmail)
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-offset-2 col-sm-10">
                                <button type="submit" class="btn btn-success">
                                    Launch Chat</button>
                            </div>
                        </div>
                    </div>
                    <div class="alert alert-info">
                        <h4>
                            Chat Hours</h4>
                        Chat hours are listed below.<br>
                        Monday - Saturday: 09.00 AM - 06.00 PM IST<br>
                        Sunday: Closed
                    </div>
                </div>
            </div>
            }
        }
        else
        {
            <div class="row">
                <div class="col-lg-12  col-centered">
                    <div id="result">
                        @if (@Model.chatConversations != null)
                        {
                            foreach (var item in Model.chatConversations)
                            {
                            @item.chatConversation<br />
                            }
                        }
                    </div>
                    <div style="display: none">
                        @Html.TextBoxFor(model => model.conversationID, new { @id = "txtConversationID" });
                        @Html.TextBoxFor(model => model.candidateID, new { @id = "txtUNCandidateID" });
                    </div>
                    <textarea cols="4" rows="4" class="col-sm-4" id="txtChatConversation"></textarea>
                </div>
            </div>
        }
    </div>
</div>


Can any one help me...
Posted
Updated 17-Nov-14 16:26pm
v2
Comments
Jineesh TR 18-Nov-14 0:20am    
Any one please help me. Let me know if the question is not clear..

1 solution

Hello,
You can do with a small trick,
For both the cases, use a common paramter for above function, let say IsJScrpit=true/false
& inside that acotion result check the value of IsJScrpit for manupulation & implement
as per your requirement.

Hope it should work.:)
 
Share this answer
 
Comments
Jineesh TR 17-Nov-14 6:57am    
The problem here is that the post method from form (ie button click)is get triggered automatically after post method from javascript file is called.
Mukesh Ghosh 17-Nov-14 7:27am    
Thats fine, use query string for paramter.
Jineesh TR 17-Nov-14 7:54am    
But I dont need to call two methods.. I need post method from Javascript only while I call corresponding method.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900