【问题标题】:On Submit, action not going to controller in MVC3在提交时,动作不会在 MVC3 中的控制器
【发布时间】:2013-12-15 06:21:16
【问题描述】:

如果我单击 cshtml 文件中的提交按钮,则操作不会转到控制器。我已经为此创建了 GET 方法。但发布仅发布方法。获取工作正常..

型号

public class ABCModel

{
    public int ABCQueryId { get; set; }
    public int UserId { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "Query cannot be more than 100 characters long.")]
    [Display(Name = "Query Title*")]
    public string QueryTitle { get; set; }

    [Display(Name = "Specific Notes *")]
    public string SpecificNotes { get; set; }
    public DateTime QueryDate;      
    public string Status { get; set; }
    public string SupportDocument { get; set; }       

}

Cshtml 页面

@using (Ajax.BeginForm("ABC", "Queries", null, new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "content", LoadingElementId = "mask" }, new { enctype = "multipart/form-data", id = "intake_form", @class = "inline-form clear_both" }))

 {
   <div id="intake_sheet_1" style="visibility: visible">
    @Html.HiddenFor(model => model.ABC.ABCQueryId)


 @{
   String errorMessage = "";
    if (Session["errorMessage"] != null)
    {
     errorMessage = Session["errorMessage"].ToString();
     }
     if (!String.IsNullOrEmpty(errorMessage))
     {
      <span class="pendingtxt" style="color:red;font-size:13px;">@errorMessage</span>
      }
   }

  <div class="form_row">
  <div class="form_col1">
  <label class="QueryPageHeadings">
     Title*</label>
    </div>
   <div class="form_col2" style="z-index: 24; position: relative;" id="queryTitle_div">
      @Html.TextBoxFor(m => m.ABC.QueryTitle, new { @class = "text QueryFormTextBox" })          <br />
    <div style="float: left">
      @Html.ValidationMessageFor(m => m.ABC.QueryTitle)
      </div>
     </div>
    </div>
     <div class="form_row">
        <div class="form_col1">
           <label class="QueryPageHeadings">
             Specific Notes</label>
         </div>
      <div class="form_col2" style="z-index: 21; position: relative;"  id="specific_note_div">
       @Html.TextAreaFor(m => m.ABC.SpecificNotes, new { @class = "text QueryFormTextBox", style = "resize:none;height:160px;" })
       <br />
       <div style="float: left">@Html.ValidationMessageFor(m => m.ABC.SpecificNotes)             </div>
       </div>
       </div>
       <div class="form_row">
        <div class="form_col1">
         <label class="QueryPageHeadings">
           Supporting Document
            </label>
           </div>
          <div class="form_col2">
            <div style="width:100%;float:left;">
               @Html.TextBoxFor(m => m.ABC.SupportDocument, new { @Id = "lblFileName", @readonly = "readonly" })
              </div>
            <div style="width:100%;float:left;">
                <button id='btnUploadPopup' type="button" style="float:left;"  onclick="javascript:OpenFileUpload();">
                   Upload Files</button>
                </div>

              </div>
            </div>
         <div class="form_row form_footer">
           <p class="indent">
               <input class="primary button float_right" value="Submit" type="submit" name="button" />

            </p>
           </div>
       </div>
   </div>

控制器

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult ABC(ABCViewModel model, string button)
    {

            QURManager manager = new QURManager();
            if (Session["userDetails"] != null)
            {
                manager = new QURManager((RegModel)Session["userDetails"]);
            }
            String errorMessage = "";
            if (String.IsNullOrEmpty(errorMessage))
            {
                errorMessage = errorMessage + " Fields can not be empty.";
            }
            if (!String.IsNullOrEmpty(errorMessage))
            {
                Session["errorMessage"] = errorMessage;
                IABC data = new ABCViewModel();

                data.ABC.QueryTitle = model.ABC.QueryTitle;
                data.ABC.SpecificNotes = model.ABC.SpecificNotes;
                data.ABC.SupportDocument = model.ABC.SupportDocument;

                TempData["isEdit"] = false;
                return View("ABC", data);
            }
            else
            {
                Session["errorMessage"] = "";
                Session.Remove("errorMessage");
            }

            Session["fileName"] = null;
            AssignSession.UpdateRegisterUserSession();

            return View();           
    }      

【问题讨论】:

  • 看起来它应该工作假设控制器名称是“QueriesController”。我将您的代码复制到一个新项目中并且可以正常工作(无法上传文件),但除此之外,请求转到了更正控制器操作。
  • @SBurris 在我的项目中,即使没有上传文件,它也无法正常工作。有什么想法吗?
  • @SajjadHashmi 你是什么意思?我没找到你?
  • @SBurris 我已经使用 VS 中的断点进行了检查。它甚至不会在后期操作中进入控制器。
  • 您的控制器名称是什么?是 QueriesController 吗?

标签: c# asp.net .net asp.net-mvc-3


【解决方案1】:

根据您的示例检查您提到的控制器名称是否正确,您的控制器名称应为QueriesController

确保您的页面中包含以下脚本:

<script src="<%= Url.Content("~/Scripts/MicrosoftAjax.debug.js") %>" type="text/javascript"></script>  
<script src="<%= Url.Content("~/Scripts/MicrosoftMvcAjax.debug.js") %>" type="text/javascript"></script>
<script src="<%= Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js") %>" type="text/javascript"></script>

在你的 web.config 你有这个礼物:

<add key="UnobtrusiveJavaScriptEnabled" value="true" /> 

【讨论】:

  • 表示 MicrosoftAjax.. 我使用过“jquery.validate.unobtrusive.min.js”
猜你喜欢
  • 2020-03-05
  • 2016-05-30
  • 2014-05-18
  • 2011-08-27
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多