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
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
$(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.");
}
});
}
});
});
My View
@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...