我有这个MVC代码:
<p><span data-bind="text: getName()" ></span></p>
这是basic.cshtml:
@section Scripts {
<script>
function ViewModel() {
var self = this;
self.name = firstName + ' ' + surName;
self.getName = function () {
return self.name;
};
var viewModel = new ViewModel('@Model.firstName', '@Model.surName');
ko.applyBindings(viewModel);
};
</script>
}
HomeController中:
namespace latever.Controllers
{
public class HomeController : Controller
{
public ActionResult Basic()
{
var person = new Person
{
firstName = "Adam",
surName = "Andersen"
};
return View(Person);
}
}
}
和我的Person.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace latever.Controllers
{
public class Person
{
public string firstName { get; set; }
public string surName { get; set; }
}
}
正如你所看到的,它看起来非常简单,我只是试图以某种方式使用ko.js打印出basic.cshtml中的名称,但是它显然不适用于Person
类
答案 0 :(得分:0)
C#区分大小写。人与人不一样
public ActionResult Basic()
{
var person = new Person
{
firstName = "Adam",
surName = "Andersen"
};
return View(person);
}