简体   繁体   中英

Passing view model from razor view to controller action

I have a problem, when using asp.net core 2.2. I have a model named "Test".

public class Test
{
    public string Email { get; set; }
}

And a controller named "UserController"with these actions:

public IActionResult Test()
{
    var test = new Test
    {
        Email = "Hamid@gmail.com"
    };
    return View(test);
}

[HttpPost]
public IActionResult Test(Test param)
{
    if (ModelState.IsValid)
    {

    }
    return View();
}

and view with named "test.cshtml" is:

@model Test
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Test</title>
</head>
<body>
    <form asp-controller="User" asp-action="Test" asp-area="Account" method="post">
        <div class="form-group row">
            <div class="col-lg-3 col-12">
                <label asp-for="Email" class="col-form-label">Email:</label>
            </div>
            <div class="col-lg-9 col-12">
                <input asp-for="Email" class="form-control email"
                       value="@Model.Email" autofocus="autofocus" />
            </div>
        </div>
        <input type="submit" value="Send" />
    </form>

</body>
</html>

When i sending model to the view, no problem, But when I click on submit button for sending form to the action,then my model is null.

图片

My problem solved.

With remove this line:

@removeTagHelper Microsoft.AspNetCore.Mvc.TagHelpers.InputTagHelper, Microsoft.AspNetCore.Mvc.TagHelpers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM