将原始 html 传递给模型并渲染它

问题描述

我想将 html 表单传递给模型,并在加载页面后 - 我使用 (Html.Raw (model) 正确生成表单,我希望创建此表单,然后使用 JS 脚本,将其发送到某个控制器)。当它尝试这样做时,它会导致页面快速重新加载。这是我的代码

助手类:

public class Content
{
    public string Name { get; set; }
    public string Data { get; set; }

public Content(string name)
{
    Name = name;
    Data = $"<form id=\"formid\" method=\"post\" asp-action=\"Submit\" class=\"form-horizontal\"><input type=\"hidden\" name=\"name\" value=\"@Model.name\"/></form>";
}

控制器:

public IActionResult Send()
{
    var res = new Content("Test");

    return View(res);
}

[HttpPost]
public IActionResult Submit(string name)
{
    return View();
}

发送.cshtml

@model Content

@Html.Raw(Model.Data)

<script>
    window.onload = function () {
    document.forms["formid"].submit();
    }
</script>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)