我一直在网站上工作,希望用户能够提交地址。 我以为会很不错,当您还没有添加地址时,请提供一个按钮,该按钮将动态创建一个表单供您提交。 一切似乎都很好,但是当我单击“提交”按钮时,页面会刷新,但是不会调用我的控制器功能。我试图设置一个断点来验证这一点。
Controller function:
public void AddAdress(string adressname, string streetname, string streetnmbr, string zipcode, string country)
{
//I breakpoint here//
string check = adressname;
}
Js function that gets called when u press add adress button:
function AddAdress()
{
var noadressdiv = document.getElementById("noadress");
divlocation = noadressdiv.getBoundingClientRect();
noadressdiv.remove();
CreateElements();
var parentdiv = document.getElementById("parent");
parentdiv.appendChild(adressdiv);
}
the CreateElements() function (this is a big one sorry)
adressdiv = document.createElement("div");
adressdiv.style.position = divlocation;
adressdiv.setAttribute("justify-content", "flex");
adressform = document.createElement("form");
adressform.setAttribute("asp-controller", "User");
adressform.setAttribute("asp-action", "AddAdress");
adressform.method = "post";
//I create all the input elements here//
btnsubmit = document.createElement("button");
btnsubmit.setAttribute("type", "submit");
btnsubmit.setAttribute("id", "submit");
btnsubmit.setAttribute("class", "btn-submit");
btnsubmit.style.width = "70%";
btnsubmit.innerHTML = "submit adress!";
adressdiv.appendChild(adressform);
adressform.appendChild(adressnameinput);
adressform.appendChild(adressstreetinput);
adressform.appendChild(adressnmbrinput);
adressform.appendChild(adresszipinput);
adressform.appendChild(adresscountryinput);
adressform.appendChild(btnsubmit);
}
我希望表单会调用controller方法,但事实并非如此。有人在乎为什么冒险吗?