简体   繁体   English

视图中的控制器在编辑后不会更新视图模型

[英]Controller in view doesn't update the view model upon being edited

I have the following form. 我有以下表格。

@model LoginVm
<form asp-controller="Sec" 
      asp-action="Authorize"
      asp-all-route-data="@Model.Querify" 
      method="post">
  Email:  <input asp-for="@Model.UserName" /> <br />
  Password: <input asp-for="@Model.Password" /><br />
  <button type="submit">Send</button>
</form>

The view model is like this. 视图模型是这样的。

public class LoginVm
{
  public string UserName { get; set; }
  public string Password { get; set; }
  public string ReturnUrl { get; set; }

  public IDictionary<string, string> Querify 
    => new Dictionary<string, string>
    {
      { "username", UserName },
      { "password", Password },
      { "returnurl", ReturnUrl }
    };
  }

However, in the receiving method shown below, only the values that are written to the view model before rendition are present. 但是,在下面显示的接收方法中,仅存在在再现之前写入视图模型的值。 So, whatever I put in the view model prior to passing it into the view, it's there. 因此,无论在将模型传递到视图之前放入视图模型中的任何内容,它都在那里。 Any changes I try to do to the model in the input boxes are lost. 我尝试在输入框中对模型进行的任何更改都将丢失。

[HttpPost]
public IActionResult Authorize(
  [FromQuery]string returnUrl, 
  [FromQuery]string userName, 
  [FromQuery] string password)
{ ... }

I though based on the docs that I bound the value from the input box to the fields in the model but apparently I have not. 我虽然基于文档 ,但将输入框的值绑定到模型中的字段,但显然没有。 Not sure how to diagnose it further. 不知道如何进一步诊断。

I think you might be binding the original data back due to the line: 我认为您可能由于以下原因而将原始数据绑定回:

      asp-all-route-data="@Model.Querify" 

I think this is sending the original model back to the controller as the parameters and ignores the ones in your form. 我认为这会将原始模型作为参数发送回控制器,而忽略了表单中的参数。

Remove that line and replace it with 删除该行并替换为

 asp-route-returnurl="@Model.ReturnUrl"

Edit: 编辑:

Updated based on comments. 根据评论进行了更新。

Tested this as working, looks like the [FromQuery] is also an issue so replaced with just taking the viewmodel back. 经过测试可以正常工作,看起来[FromQuery]也是一个问题,因此被取回了viewmodel代替了。

Controller code: 控制器代码:

  public IActionResult Sec()
    {
        var viewModel = new LoginVm();

        viewModel.ReturnUrl = "http://stackoverflow.com";
        return View(viewModel);
    }

    [HttpPost]
    public IActionResult Sec(LoginVm viewModel)
    {
       return Redirect(viewModel.ReturnUrl);
    }

ViewModel: 视图模型:

     public class LoginVm
{
    public string UserName { get; set; }
    public string Password { get; set; }
    public string ReturnUrl { get; set; }
}

View: 视图:

 @model LoginVm
<form asp-controller="Authorization"
  asp-action="Sec"   
asp-route-returnurl="@Model.ReturnUrl"
  method="post">
Email:  <input asp-for="@Model.UserName" /> <br />
Password: <input asp-for="@Model.Password" /><br />
<button type="submit">Send</button>
</form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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