我仔细研究一下SO,看看看是否曾问过此事,是否找不到任何东西(因此,如果在我道歉之前确实曾问过)。
在我试图做些什么时,如果用户想要掌握的技能在名单上,那么他们就可以在数据库中添加这种技能,那么用户可以从他们的技能清单中挑选,而我用WCF &、j Query AJAX完成了这项工作。 这部法典:
$("#AddNewSkill").click(function () {
$("#AddNewSkill").attr("disabled", true);
$("#newSkill").attr("disabled", true);
var newSkill = $("#newSkill").val();
var data = { name: $("#newSkill").val(), description: "", type: "Skill" };
data = JSON.stringify(data)
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "../WeddingPhotographerService.svc/AddNew",
data: data,
dataType: "json",
success: function () {
successCall( #newSkill , #AddNewSkill );
//$( #SkillListViewContainer ).load( ../Account/GetSkillControl );
},
error: function (msg) {
$("#AddSkillError").text(msg.d);
$("#AddSkill").attr("disabled", false);
$("#NewSkill").attr("disabled", false);
}
});
});
此处采用AJAX-Enabled WCF服务的方法:
[OperationContract]
public bool AddNew(string name, string description, string type)
{
switch (type)
{
case "":
goto default;
case "skill":
IRepository<Skill> skillRepo = ObjectFactory.GetInstance<IRepository<Skill>>();
var skill = new Skill { Name = name, Description = (string.IsNullOrEmpty(description)) ? string.Empty : description };
skillRepo.Save(skill);
return true;
case "equipment":
IRepository<Equipment> eqRep = ObjectFactory.GetInstance<IRepository<Equipment>>();
var eq = new Equipment { Name = name, Description = (string.IsNullOrEmpty(description)) ? string.Empty : description };
eqRep.Save(eq);
return true;
case "occasion":
IRepository<Occassion> occRepo = ObjectFactory.GetInstance<IRepository<Occassion>>();
var oc = new Occassion { Name = name, Description = (string.IsNullOrEmpty(description)) ? string.Empty : description };
occRepo.Save(oc);
return true;
default:
IRepository<Skill> repo = ObjectFactory.GetInstance<IRepository<Skill>>();
var s = new Skill { Name = name, Description = (string.IsNullOrEmpty(description)) ? string.Empty : description };
repo.Save(s);
return true;
}
}
很奇怪,但是,如果我有这2个部分工作的话,我会最优化。 The how the ListBox is beingloaded in the view:
<%: Html.ListBox("Skills", Model.SkillList, new { @style = "width:157px; height:90px;background:#e2f0f1;", @size = "3", @class = "inputbox" })%>
起源于Registration ModelView.cs,载于我的模型:SricList:
private MultiSelectList GetSkills(string[] selectedValues)
{
List<Skill> list = new List<Skill>();
IRepository<Skill> skills = ObjectFactory.GetInstance<IRepository<Skill>>();
foreach (Skill skill in skills.GetAll())
{
list.Add(new Skill()
{
Key = skill.Key,
Name = skill.Name,
Description = ""
});
}
return new MultiSelectList(list, "Key", "Name", selectedValues);
}
页: 1 意见
[MoveFormsScript]
public ActionResult Index()
{
return View(new RegistrationModelView());
}
我完全相信我所贴出的所有代码(除了以WCF服务添加这一新项目,而花上述服务费的颜色无关紧要,但我认为我提供了尽可能多的信息)。
同我一样,数据库中增加了新的价值,没有问题,我的问题是更新名单Box,以反映新的价值。 没有人会提出想法,能够帮助这样做?