【问题标题】:How do I use post method to display a content in ASP.net core MVC如何使用 post 方法在 ASP.net core MVC 中显示内容
【发布时间】:2021-07-28 02:35:58
【问题描述】:

我对 ASP.net 编程相当陌生,但我仍然熟悉 C# 和 C 语言。我最初被教导如何浏览 ASP.net Razor 页面。这是简单易行的。但是对于我的最后一个项目,我继续做了一个 ASP.net 核心 MVC 页面。到目前为止,它很好并且可以工作(工作原理相似),除了后端部分。在 razor 页面中,页面是在一起的,index.cshtml 和 index.cshtml.cs 在一个地方,但在 MVC 中并非如此。我很难理解如何在我的页面中使用method="post"

我的剃须刀查看页面代码:

@{
    ViewData["Title"] = "Lorem Ipsum Generator";
    Layout = "/Views/Shared/_Layout.cshtml";
}


<style>
    body {
        /* Margin bottom by footer height */
        margin-bottom: 60px;
        font-family: Kalam, cursive;
    }

    nav {
        height: 100%;
        width: 100%;
        left: 0;
        right: 0;
        top: 0;
        bottom: 0;
        position: absolute;
        background: linear-gradient(135deg, #a8e6cf, #dcedc1, #ffd3b6, #ffaaa5, #ff8b94);
        background-size: 200% 200%;
        animation: rainbow 10s alternate infinite;
    }
    .JK {
        padding: 15px 25px;
        font-size: 24px;
        text-align: center;
        cursor: pointer;
        outline: none;
        color: #fff;
        background-color: #04AA6D;
        border: none;
        border-radius: 15px;
        box-shadow: 0 9px #999;
    }
    .JK:hover {
        background-color: #3e8e41
    }
    .JK:active {
        background-color: #3e8e41;
        box-shadow: 0 5px #666;
        transform: translateY(4px);
    }
</style>

<html>
<body class="text-center">
    <form method="post">
        <h1 style="font-size:26px;">Lorem Ipsum Generator</h1>
        <p>At this page, You will get to generate Lorem Ipsum text, at your desired size and format. Enjoy!</p>
        <p>Enter Number of words to generate: <input id="wordx" type="text" value="10" /></p>
        <p>Enter Number of paragraphs to generate: <input id="parax" type="text" value="2" /></p>
        <p>Include Data and time at the end of the list? (Proxy is allowed!)  <input type="radio" id="Y" name="Dt" />Yes    <input type="radio" id="N" name="Dt" checked="checked" />No</p>
        <p>If you have selected the option "Yes", Please choose a specific time and date: <input type="datetime-local" id="datetimex" /></p>
        <p>Do you want the generator to start with "Lorem ipsum dolor sit amet" (Check the box, only if you want): <input type="checkbox" id="startx" />Yes</p>
        <p><input type="submit" class="JK" value="Generate!" /></p>
    </form>
</body>
</html>

我查看了这个页面:How to call POST method in .NET CORE MVC directly from Razor View? 寻求解决方案,但它并没有像想象的那样锻炼。我正在使用这个 Lorem Lpsum 生成器:https://github.com/EdCharbeneau/Prototyping

任何帮助将不胜感激:)

【问题讨论】:

标签: c# post razor asp.net-core-mvc


【解决方案1】:

如果您想知道如何使用表单方法将值发布到 MVC 控制器中,您可以了解什么是 MVC。

MVC 表示模型视图和控制器。

MVC 是基于控制器构建的。每个控制器都会包含很多动作方法。

每个方法都包含自己的视图。

每个视图都有自己的模型。

在 asp.net core MVC 中,我们通常使用表单标签助手将请求发布到后端 MVC 控制器。

例如:

MVC Home 控制器包含一个名为 home 的方法和 post 方法 test

    public IActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public IActionResult Test(int  i) {

        return Ok();
    }

如果我想将 i 参数发布到后端,我应该修改 View 以添加以下代码:

<form asp-action="test" asp-controller="Home" method="post">
    <input id="test" name="i" type="text" />
    <input type="submit" value="test" />

</form>

如果您点击按钮,它会将输入 i 的值发布到后端测试方法。如下:

此外,Lorem Lpsum 生成器似乎是为 ASP.NET MVC 而不是 asp.net 核心构建的。这可能在 asp.net 5/asp.net core 中不起作用。

【讨论】:

  • 如果您觉得我的回复对您有帮助,请标记为回答。这样其他面临同样问题的人可以更容易地找到答案。谢谢。
猜你喜欢
  • 2016-08-05
  • 2022-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-08
  • 1970-01-01
  • 2020-03-29
  • 1970-01-01
相关资源
最近更新 更多