如何预先填充模型的数据以形成我正在模型中获取数据,我可以在 Quickwatch 中看到,但它没有填充到表单中。
这是我的模型和视图。我还从这个模型发送数据,它在那里工作正常,我真的是 ASP.NET MVC 的新手:
namespace UserMaster.Models
{
public class UserMasterClass
{
public int UserId { get; set; }
public string UserName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Gender { get; set; }
public string MobileNo { get; set; }
public string Domain { get; set; }
public string WinUserId { get; set; }
public bool IsActive { get; set; }
}
public class UserMasterSaveClass
{
public UserMasterClass UserGetData {get; set;}
public string Password { get; set; }
public string IPAddress { get; set; }
public int CreatedBy { get; set; }
public DateTime CreatedOn { get; set; }
public int ModifiedBy { get; set; }
public DateTime ModifiedOn { get; set; }
}
public class UsersViewModel
{
public List<UserMasterClass> UserMasterData { get; set; }
public UserMasterSaveClass NewUserData { get; set; }
}
}
局部视图是
@model UsersViewModel
<!-- Insert new users -->
@await Html.PartialAsync("Index", Model.NewUserData)
<!-- Display existing users -->
@await Html.PartialAsync("UsersData", Model.UserMasterData)
最终视图是
@{
ViewData["Title"] = "Home Page";
@model UserMasterSaveClass
}
<div class="form-group">
<label for="UserName">UserName</label>
@Html.TextBoxFor(m => m.UserGetData.UserName, new { @class = "form-control", id = "UserName" })
</div>
->预填充模型的数据以形成我正在模型中获取数据
您的意思是您希望表单在渲染时包含原始数据吗?如果是这样,我下面的示例可能会对您有所帮助。在父页面中,我们使用包含所需表单数据的视图模型。当使用 html helper 渲染部分视图时,我们同时传递了数据,它是视图模型的实体。
---- Index.cshtml ----
@model UsersViewModel;
@await Html.PartialAsync("_Index", Model.NewUserData)
@await Html.PartialAsync("_UsersData", Model.UserMasterData)
---- Controller: ----
public IActionResult Index()
{
var model = new UsersViewModel
{
UserMasterData = new List<UserMasterClass> {
new UserMasterClass {
UserId = 1,
UserName = "user1",
},
new UserMasterClass {
UserId = 2,
UserName = "user2",
}
},
NewUserData = new UserMasterSaveClass
{
Password = "password",
IPAddress = "192.168.1.1",
UserGetData = new UserMasterClass
{
UserId = 1,
UserName = "user1",
}
}
};
return View(model);
}
---- Partial view _UsersData.cshtml ----
@model IEnumerable<UserMasterClass>;
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.UserId)
</th>
<th>
@Html.DisplayNameFor(model => model.UserName)
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.UserId)
</td>
<td>
@Html.DisplayFor(modelItem => item.UserName)
</td>
</tr>
}
</tbody>
</table>
---- Partial View _Index.cshtml ----
@model UserMasterSaveClass;
<form asp-action="New">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="UserGetData.UserId" class="control-label"></label>
<input asp-for="UserGetData.UserId" class="form-control" />
<span asp-validation-for="UserGetData.UserId" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="UserGetData.UserName" class="control-label"></label>
<input asp-for="UserGetData.UserName" class="form-control" />
<span asp-validation-for="UserGetData.UserName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="IPAddress" class="control-label"></label>
<input asp-for="IPAddress" class="form-control" />
<span asp-validation-for="IPAddress" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</form>