【问题标题】:HTML.BeginForm doesn't call the proper actionHTML.BeginForm 没有调用正确的操作
【发布时间】:2014-06-30 22:43:33
【问题描述】:

我在Edit.chtml中有一个这部分看起来像

@using (@Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
<article class="SearchBoxCon clearfix">
                        @if (Model.Entity.Document == null)
                        {
                            using (Html.BeginForm("AudioUpload", "Noun", FormMethod.Post, new { enctype = "multipart/form-data", NounId = Model.Entity.Id }))
                            {
                                <article class="BtnCon">

                                    <article class="BrowseBtn">
                                        <input type="file" value="Browse" name="file">
                                    </article>
                                    <article class="BrowseUpload">
                                        <input type="submit" id="AudioUpload" value="AudioUpload">
                                    </article>

                                </article>


                            }

                        }
                        else
                        {
                            <article class="inputBTn">
                                <input type="submit" value="Play">
                                <input type="submit" value="Delete">
                            </article>
                        }
                    </article>

                <article class="inputBTn">
                    <input type="submit" value="Save">
                </article>
                <!-- submit btn ends here -->
                @Html.ActionLink("Back to List", "Index", "Noun/Index", null, new { @class = "BackList" })
            </article>


}

浏览并上传文件后,点击上传,它会触发编辑发布操作,而不是看起来像的上传音频

 [HttpPost]
        public ActionResult AudioUpload(HttpPostedFileBase file , int NounId )
        {

【问题讨论】:

  • 您认为还有其他形式吗?例如else {...}if 之后?
  • 这里有edit的主要形式@using (@Html.BeginForm()),里面包含了所有的字段
  • 所以,Model.Entity.Document 不为空...
  • 您的代码工作正常。您的条件逻辑不正确...
  • 我修改了问题以显示全局

标签: asp.net-mvc-4


【解决方案1】:

[ActionName("Edit")] 将属性放在您的操作方法上它将起作用

[HttpPost, ActionName("Edit")]
    public ActionResult AudioUpload(HttpPostedFileBase file , int NounId )
    {

如果您的视图名称是 Edit.cshtml,请编写这样的代码

【讨论】:

  • 当前对控制器类型“NounController”的操作“编辑”请求在以下操作方法之间不明确:System.Web.Mvc.ActionResult AudioUpload(System.Web.HttpPostedFileBase, Int32) 类型为 ArabicELearning .Controllers.NounController System.Web.Mvc.ActionResult Edit(Int32, System.Web.Mvc.FormCollection) on type ArabicELearning.Controllers.NounController
【解决方案2】:

这样试试,

@if (Model.Entity.Document == null)
 {
  using (Html.BeginForm("AudioUpload", "Noun", FormMethod.Post, new { enctype = "multipart/form-data", NounId = Model.Entity.Id,Id="frmAudioUpload" }))
  {<article class="BtnCon">

    <article class="BrowseBtn">
    <input type="file" value="Browse" name="file">
    </article>
    <article class="BrowseUpload">
    <input type="button" id="AudioUpload" value="AudioUpload">
    </article>

    </article>
}}

脚本

<script type="text/javascript">
    $(function(){
      $('#AudioUpload').click(function(){
        $('#frmAudioUpload').submit();
      });
    });
</script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-03
    • 2020-05-27
    相关资源
    最近更新 更多