簡體   English   中英

通過Ajax在MVC控制器上找不到404

[英]Getting a 404 not found on mvc controller via ajax

我在嘗試加載的部分視圖中找不到404,我正在使用控制器來決定要加載的內容

[HttpGet]
public PartialViewResult GetPartialView()
{
  //Add model if any and send it through partialview
  return PartialView("_PartnerDetailsForm.cshtml");
}

在此處輸入圖片說明

在我的腳本文件中,這是我用來加載partiview的ajax項目,但在調試時卻遇到未找到的錯誤404。

$(document).on('change', '#chkisJointApplication', function () {
        if (this.checked) {
            $.ajax({
                url: '@Url.Action("GetPartialView", "FormsController")',
                type: "Get",
                success: function (data) {
                    $("#partnerForm").html(data);
                }
            });

        }
        else
            $("#partnerForm").html(''); //clearing the innerHTML if checkbox is unchecked
  });

控制台窗口調試 在此處輸入圖片說明

屏幕截圖顯示了Forms Controller。 在此處輸入圖片說明

編輯1可行,但現在我面臨以下問題,怎么辦

在此處輸入圖片說明

編輯2

這是為了顯示部分視圖的表單內容。

<div class="row">

 <section class="col col-6">

     <label class="input">

        <i class="icon-prepend fa fa-user"></i>
        <input type="text" name="fname" placeholder="First name">
    </label>
</section>
<section class="col col-6">
    <label class="input">
        <i class="icon-prepend fa fa-user"></i>
        <input type="text" name="lname" placeholder="Last name">
    </label>
</section>
</div>
<div class="row">
 <section class="col col-6">
    <label class="input">
        <i class="icon-prepend fa fa-envelope"></i>
        <input type="email" name="email" placeholder="E-mail">
    </label>
</section>
<section class="col col-6">
    <label class="input">
        <i class="icon-prepend fa fa-phone"></i>
        <input type="tel" name="phone" placeholder="Phone">
    </label>
</section>
</div>

您不需要修復代碼js,例如:

$(document).on('change', '#chkisJointApplication', function () {
        if (this.checked) {
            $.ajax({
                url: '/Forms/GetPartialView',
                type: "Get",
                success: function (data) {
                    $("#partnerForm").html(data);
                }
            });

        }
        else
            $("#partnerForm").html(''); //clearing the innerHTML if checkbox is unchecked
  });

您不需要寫“控制器”一詞。

url: '@Url.Action("GetPartialView", "Forms")'

編輯

如果您將其保存在.js文件中:

將此放在視圖中:

<input type="hidden" id="myUrl" value = "@Url.Action("GetPartialView", "Forms")" />

並在js中:

url: $("#myUrl").val(),

要么

url: document.getElementById('myUrl').value,

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM