【发布时间】:2015-04-23 21:32:12
【问题描述】:
所以,我的应用程序中有 2 个 C# 实体。我计划使用相同的淘汰视图模型将这 2 个实体绑定到视图。模型如下
public class Provider
{
public int ProviderID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public ProviderDetails ProviderDetails { get; set; }
}
public class ProviderDetails
{
public int ProviderDetailsId { get; set; }
public string Certification { get; set; }
public string Specialization { get; set; }
public string TaxonomyCode { get; set; }
public int ProviderId { get; set; }
}
我有以下 HTML
<body>
<div class="container">
<h1 class="col-sm-offset-3">Enter Provider Details:</h1>
<form class="form-horizontal" role="form" id="providerDetailsForm" method="post">
<div class="form-group">
<label class="col-sm-2 control-label labelfont">First Name:</label>
<div class="col-sm-6">
<input type="text" class="form-control" placeholder="Enter the First Name" id="firstName" data-bind="value: firstName, @*hasFocus: setTheFocusaAfterReset*@ event: { keypress: allowOnlyAlphabets }" name="firstName" maxlength="20">
<span class="col-sm-4 error"></span>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label labelfont">Last Name:</label>
<div class="col-sm-6">
<input type="text" class="form-control" placeholder="Enter the Last Name" id="lastName" name="lastName" data-bind="value: lastName, event: { keypress: allowOnlyAlphabets }" maxlength="20">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label labelfont">Certification:</label>
<div class="col-sm-6">
<select class="form-control" id="certification" name="certification" data-bind="value: certification, options: certificationArray, optionsCaption: 'Select a Certification'">
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label labelfont">Specialization:</label>
<div class="col-sm-6">
<select class="form-control" id="specialization" name="specialization" data-bind="value: specialization, options: specializationArray, optionsCaption: 'Select a Specialization'">
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label labelfont">Taxonomy Code:</label>
<div class="col-sm-6">
<input type="text" class="form-control" placeholder="Taxonomy code" id="taxonomyCode" name="taxonomyCode" data-bind="textInput: taxonomyCode" readonly>
</div>
</div>
</form>
</div>
</body>
这里的淘汰绑定是当有单个实体提供者时。我必须拆分实体以显示数据库中表之间的父子关系。关系是一对一的。 我正在考虑像这样创建一个单一的视图模型。
var ProviderViewModel
{
var self = this;
self.providerID = ko.observable("");
self.firstName = ko.observable("");
self.lastName = ko.observable("");
//This is where I am facing difficulty.How do I include the Providerdetails within this viewmodel.
}
现在,我有 Providerdetails 的代码,像这样
self.specializationArray = ko.observableArray([]);
self.certificationArray = ko.observableArray(["M.B.B.S", "M.D.", "R.N.", "M.S.N."]);
self.certification = ko.observable("");
self.specialization = ko.observable("");
但是我如何将它包含在同一个视图模型中。我有点困惑。请指导我正确的方向。
用于创建的 MVC 控制器代码。
[HttpPost]
public ActionResult CreateProvider(Provider provider)
{
try
{
int providerCreationSuccessful = _repository.CreateProvider(provider);
if (providerCreationSuccessful == 1)
TempData["userIntimation"] = "Provider Registered Successfully";
return RedirectToAction("ShowTheListOfProviders");
}
catch (Exception Ex)
{
_logger.Error(Ex.Message);
return View("Error");
}
}
【问题讨论】:
标签: c# asp.net-mvc-4 mvvm knockout.js