提问人:IAMABANANA 提问时间:5/11/2023 最后编辑:Sir RufoIAMABANANA 更新时间:5/11/2023 访问量:32
当 RedirectToAction 转到第二页时,嵌套视图模型被擦除
Nested view model erased when RedirectToAction goes to second page
问:
一段时间以来,我一直在用头撞墙,我不知道为什么这不起作用,通常我没有问题。我想我已经看了这么久了,我再也想不出答案了,所以非常感谢帮助!
我在一页上加载了 3 个视图模型,然后使用一个 我将我的字段提交到控制器方法,对某些字段和“您的表单已成功提交”确认页面执行一些三元表达式,然后我想在其中显示他们刚刚提交的上述字段。BeginForm
RedirectToAction
视图模型字段在函数中输入得很好,但随后 2 个嵌套的视图模型数据消失了。当它在控制器后方法中时,我清楚地看到了数据,并且我通过 传递了模型,但是一旦我进入验证视图,它就消失了?Submit
RedirectToAction
我的视图模型:
public class MainViewModel
{
public Guid Id { get; set; }
public string? Name { get; set; }
public string? Email {get; set;}
public TypesViewModel TypeViewModel {get; set;}
}
public class TypesViewModel
{
public Guid Id { get; set; }
public Guid ExternalId {get; set;}
public bool Type {get; set;}
public string? Name { get; set; }
public CustomFieldsViewModel CustomFieldViewModel {get; set;}
}
public class CustomFieldsViewModel
{
public bool Email { get; set; }
public bool Phone {get; set;}
public bool Text {get; set;}
public string? EmailContact { get; set; }
public string? PhoneNumberContact {get; set;}
public string? TextMessageContact {get; set;}
public String? PhoneNumber {get; set;}
}
我的控制器:
public class PrimaryController : Controller
{
public IActionResult Index()
{
MainViewModel mainViewModel = new MainViewModel();
TypesViewModel typesViewModel = new TypesViewModel();
mainViewModel.TypeViewModel = typesViewModel;
CustomFieldsViewModel customFieldsViewModel = new CustomFieldsViewModel();
mainViewModel.TypeViewModel.CustomFieldViewModel = customFieldsViewModel;
return View(mainViewModel);
}
[HttpPost]
public ActionResult Submit(MainViewModel model)
{
model.Id = Guid.NewGuid();
model.TypeViewModel.Id = Guid.NewGuid();
model.TypeViewModel.ExternalId = Guid.NewGuid();
model.TypeViewModel.Name = model.TypeViewModel.Type == true ? "Yes, Contact me." : "No, Do not contact me.";
model.TypeViewModel.CustomFieldViewModel.EmailContact = model.TypeViewModel.CustomFieldViewModel.Email == true ? "Contact by Email" : "Do Not Contact By Email";
model.TypeViewModel.CustomFieldViewModel.PhoneNumberContact = model.TypeViewModel.CustomFieldViewModel.Phone == true ? "Contact by Phone Number" : "Do Not Contact By Phone Number";
model.TypeViewModel.CustomFieldViewModel.TextMessageContact = model.TypeViewModel.CustomFieldViewModel.Text == true ? "Contact by Text Message" : "Do Not Contact By Text Message";
return RedirectToAction("Verification", model);
}
public ActionResult Verification(MainViewModel model)
{
return View(model);
}
}
我的观点:
@model MainViewModel
@{
ViewData["Title"] = "Primary Output";
}
<div class="container d-flex align-items-center justify-content-center">
<div class="box">
@using (Html.BeginForm("Submit", "Primary", FormMethod.Post))
{
<label>Name</label>
@Html.TextBoxFor(m => m.Name, new { @class = "form-control"})
<label>Email Address</label>
@Html.TextBoxFor(m => m.Email, new { @class = "form-control", onblur="checkEmail(this.value)", Type = "email"})
<label>Can we contact you?</label>
@Html.CheckBoxFor(m => m.TypeViewModel.Type)
<span>Yes</span>
<div class="contact-options">
<label>Prefered form of communication?</label>
<ul class="list">
<li> <input type="checkbox" value="true" name="Model.TypeViewModel.CustomFieldViewModel.Email" id="Email">
<span>Email</span>
</li>
<li> <input type="checkbox" value="true" name="Model.TypeViewModel.CustomFieldViewModel.Phone" onclick="ShowHideDiv(this)" id="Phone">
<span>Phone</span>
</li>
<li> <input type="checkbox" value="true" name="Model.TypeViewModel.CustomFieldViewModel.Text" onclick="ShowHideDiv(this)" id="Text">
<span>Text</span>
</li>
</ul>
</div>
<div id="Phone-Number">
<label>Phone Number</label>
<input class="form-control" type="tel" name="Model.TypeViewModel.CustomFieldViewModel.PhoneNumber" />
</div>
<div class="button-container">
<input id="Submit" type="Submit" value="Submit" />
</div>
}
</div>
</div>
——
@model MainViewModel
@{
ViewData["Title"] = "Verification";
}
<div class="container d-flex align-items-center justify-content-center">
<div class="circular-square">
<img src="https://i.ibb.co/YpsHjmW/The-Big-Lebowski-hp-GQ-25-Feb16-rex-b.jpg" border="0">
</div>
</div>
<div class="verification-container d-flex align-items-center justify-content-center">
<div class="verification-title">The Dude Abides.</div>
</div>
<div class="verification-container d-flex align-items-center justify-content-center">
<div class="box">
<p>Congrats! You've made it to the final round. Below is the information you submitted!</p>
<p><label>Id:</label> @Model.Id</p>
<p><label>Name:</label> @Model.Name</p>
<p><label>Email:</label> @Model.Email</p>
<p><label>Type Id:</label> @Model.TypeViewModel.Id</p>
<p><label>Type Name:</label> @Model.TypeViewModel.Name</p>
<p><label>Custom Field 1:</label> @Model.TypeViewModel.CustomFieldViewModel.PhoneNumberContact</p>
<p><label>Custom Field 2:</label> @Model.TypeViewModel.CustomFieldViewModel.EmailContact</p>
<p><label>Custom Field 3:</label> @Model.TypeViewModel.CustomFieldViewModel.TextMessageContact</p>
@if(@Model.TypeViewModel.CustomFieldViewModel.PhoneNumber != null) {
<p><label>Phone Number:</label> @Model.TypeViewModel.CustomFieldViewModel.PhoneNumber</p>
}
<p><label>External Id:</label> @Model.TypeViewModel.ExternalId</p>
</div>
</div>
答: 暂无答案
评论
name="Model.TypeViewModel...
=>asp-for="TypeViewModel..."
或者至少 MVC 不知道如何处理名为name="TypeViewModel..."
Model...
RedirectToAction
由 GET 请求完成,第二个参数是转换为字典的路由值,并且仅使用具有简单类型的属性。嵌套类型的数据只能通过 POST 方法传输