本文介绍了如何在控制器MVC4中使用两个post方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 大家好, 我正在使用MVC应用程序。我必须在我的控制器中使用两个post方法。一个post方法需要从视图中调用,另一个需要从javascript文件中调用。 我在这里写代码。 在控制器中 public class ChatRegisterController:Controller { public ActionResult Index() { Chat register = new Chat(); return 查看(注册); } SMWebDefaultConnection objSMWebDefaultConnection = new SMWebDefaultConnection(); [HttpPost] public ActionResult索引(聊天记录) { 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 查看(注册); } [AcceptVerbs(HttpVerbs.Post)] public ActionResult SubmitChat( string unCandidateID, string conversationID, string conversationText) { objSMWebDefaultConnection.smConn.spChatConversation(conversationID,unCandidateID.getInteger(),conversationText); objSMWebDefaultConnection.smConn.SaveChanges(); 聊天聊天= 新聊天(); 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); } } 在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,成功:函数(数据){ window.location.reload( true ); },错误:函数(){ alert( 无法提交聊天。请重试。); // window.location.reload(true); } }); } }); }); 我的观点 @ model SM_WEB.Models.Chat @using SM_WEB.Models; @ { ViewBag .Title = 圣莫尼卡 - 聊天注册; 布局= 〜/查看/共享/ _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 类 = 容器 style = padding:130px 0 0 0; & gt; @if( string .IsNullOrEmpty(Model.conversationID)) { using (Html.BeginForm()) { Html.ValidationSummary( true ); < div class = row > < div class = col-lg-4面板 - 默认以col为中心 > < div 类 = 表单-horizontal top-buffer role = form > < div 类 = form-group > < div style = display:none > @ Html.TextBoxFor(model = > model.conversationID, new {@id = txtConversationID}); < / div > < label for = inputEmail3 class = col-sm -2 control-label > 名称< / label > < div class = col-sm-8 > @ Html.TextBoxFor(model = > model.unCandidateFirstName, n ew {@class = form-control,占位符= 名称}) @ Html.ValidationMessageFor(model = > ; model.unCandidateFirstName) < / div > < / div > < div 类 = form-group > < label for = inputEmail3 class = col-sm-2 control-label > 电话< / 标签 > < div class = col-sm-8 > @ Html.TextBoxFor(model = > model.unCandMobilePhone, new {@class = form-control,占位符= Mobile No}) @ Html.ValidationMessageFor(model = > model.unCandMobileP () < / div > < / div > < div class = form-group > < label for = inputPassword3 class = col-sm-2 control-label > 电子邮件< / 标签 > ; < div class = col -sm-8 > @ Html.TextBoxFor(model = > model.unCandEmail, new {@class = 表单控件,占位符= 电子邮件}) @Html .ValidationMessageFor(model = > model.unCandEmail) < / div > < / div > < div class = form-group > < div class = col-sm-offset-2 col-sm-10 > < button type = 提交 class = btn btn-success > ; 启动聊天< / 按钮 > < / div > < / div > < / div > < div class = alert alert-info > < h4> 聊天时间< / h4 > 聊天时间如下所示。< br> 星期一 - 星期六: 09 。 00 AM - 06 。 00 PM IST< br> 星期日:关闭 < / div > < / div > < / div > } } 其他 {< 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 > 任何人都可以帮助我... 解决方案 (function(){ (' #txtChatConversation')。keypress(函数(e){ var key = e.which || e.keyCode; if (key == 13 ){ .ajax({ type: POST, url: / ChatRegister / SubmitChat, data: {'unCandidateID': + 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 Controllerpublic 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."); // window.location.reload(true); } }); } });});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... 解决方案 (function () {('#txtChatConversation').keypress(function (e) { var key = e.which || e.keyCode; if (key == 13) {.ajax({ type: "POST", url: "/ChatRegister/SubmitChat", data: "{'unCandidateID':" + 这篇关于如何在控制器MVC4中使用两个post方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-27 11:49