我是淘汰赛的新手。我想将一个文本框绑定到一个viewmodel。我有以下代码,但它不起作用。有人指出我正确的方向。
我正在尝试将视图模型中的数据发布到控制器,但值为null,即使我已在文本框中输入值
<div class="container">
<form class="form-horizontal" id="ProviderForm" method="post">
<div class=" form-group">
<label for="Provider" class="control-label col-sm-2">PROVIDER :</label>
<div class="col-sm-8">
<div class="input-group">
<div class="input-group-btn">
<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul id="demolist" class="dropdown-menu">
<li><a href="#">Doctor</a></li>
<li class="disabled"><a href="#">Facilities</a></li>
</ul>
</div>
<input type="text" id="datebox" data-bind="value: ProviderType" class="form-control" />
</div>
</div>
</div>
<div class=" form-group">
<label for="firstName" class="col-sm-2 control-label">First Name</label>
<div class="col-sm-8">
<input type="text" class="form-control" id="inputFirstName" data-bind="value: FirstName" placeholder="Enter the First Name">
</div>
</div>
<div class="form-group">
<label for="lastName" class="col-sm-2 control-label">Last Name</label>
<div class="col-sm-8">
<input type="text" class="form-control" id="inputLastName" data-bind="value: Lastname" placeholder="Enter the Last Name">
</div>
</div>
<div class="form-group">
<label for="Certification" class="control-label col-sm-2">Certification :</label>
<div class="col-sm-8">
<div class="input-group">
<div class="input-group-btn">
<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul id="demolist2" class="dropdown-menu">
<li><a href="#">M.D.</a></li>
<li class="disabled"><a href="#">D.O.</a></li>
</ul>
</div>
<input type="text" id="datebox2" data-bind="value: Certification" class="form-control" />
</div>
</div>
</div>
<div class="form-group">
<label for="Specialization" class="control-label col-sm-2">Specialization :</label>
<div class="col-sm-8">
<div class="input-group">
<div class="input-group-btn">
<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul id="demolist3" class="dropdown-menu">
<li><a href="#">Family Practitioner</a></li>
<li class="disabled"><a href="#">Dentist</a></li>
<li class="disabled"><a href="#">Emergency Doctor</a></li>
</ul>
</div>
<input type="text" id="datebox3" data-bind="value: Specialization" class="form-control" />
</div>
</div>
</div>
<div class="form-group">
<label for="SSN" class="col-sm-2 control-label">SSN</label>
<div class="col-sm-8">
<input type="text" class="form-control" id="inputSSN" data-bind="value: SSN" placeholder="Enter your SSN">
</div>
</div>
<div class="form-group">
<label for="FacilityName" class="col-sm-2 control-label">Facility Name</label>
<div class="col-sm-8">
<input type="text" class="form-control" id="inputFacility" data-bind="textInput: FacilityName" placeholder="Enter your Facility">
</div>
</div>
<div class="form-group">
<label for="ContactNumber" class="col-sm-2 control-label">Contact Number</label>
<div class="col-sm-8">
<input type="text" maxlength="10" class="form-control" id="inputContactNum" data-bind="value: ContactNumber" placeholder="Enter your Facility">
</div>
</div>
<div class="form-group">
<label for="EmailID" class="col-sm-2 control-label">Email ID</label>
<div class="col-sm-8">
<input type="text" class="form-control" id="inputEmail" data-bind="value: ContactEmail" placeholder="Enter your Email ID">
</div>
</div>
<div class="form-actions span7 text-center">
<button type="submit" id="Submit" class="btn btn-primary btn-md ">Save</button>
<button type="reset" class="btn btn-primary btn-md ">Reset</button>
</div>
</form>
</div>
我的剧本
$("document").ready(function () {
$("#Submit").on("click",function(){
var Provider =
{
ProviderType: ko.observable(""),
FirstName: ko.observable(""),
Lastname: ko.observable(""),
Certification: ko.observable(""),
Specialization: ko.observable(""),
SSN: ko.observable(""),
ContactNumber: ko.observable(""),
ContactEmail: ko.observable(""),
FacilityName : ko.observable("")
}
ko.applyBindings(Provider);
$.ajax({
type: "POST",
url: "/PatientRegister/Create",
data : Provider
});
});
});
在控制器中,我使用此提供程序作为参数。
public ActionResult Create(Provider provider)
{
//....
}
目前,提供程序字段为null。我还创建了一个具有相同参数名称的Provider类
public class Provider
{
public string ProviderType { get; set; }
public string FirstName { get; set; }
public string Lastname { get; set; }
public string Certification { get; set; }
public string Specialization { get; set; }
public string SSN { get; set; }
public string ContactNumber { get; set;}
public string ContactEmail { get; set; }
public string FacilityName { get; set; }
}
答案 0 :(得分:1)
您希望此代码如何运作?
您的页面加载
时需要观察provider
对象
var Provider =
{
ProviderType: ko.observable(""),
FirstName: ko.observable(""),
Lastname: ko.observable(""),
Certification: ko.observable(""),
Specialization: ko.observable(""),
SSN: ko.observable(""),
ContactNumber: ko.observable(""),
ContactEmail: ko.observable(""),
FacilityName : ko.observable("")
}
ko.applyBindings(Provider);
点击提交按钮,将此对象发布到您的控制器。
所以敲门循环的工作方式如何,一旦你说ko.applyBindings(Provider)
它会开始观察你的控件,每当你改变控件中的任何值时,knockout会自动更新你控件的相关属性。