У меня странный опыт с NET MVC и Javascript. Сначала я попытаюсь объяснить основные вещи, которые являются существенными элементами.
CommandModel.cs
public class CommandModel
{
public string Id { get; set; }
public bool IsRequired{ get; set; }
}
ExampleModel .cs
public class ExampleModel
{
public List<CommandModel> Commands { get; set; }
}
_Commands.cs html
@model ExampleModel
@foreach (CommandModel command in Model.Commands)
{
<button type="button" onclick="command_onClick('@command.Id', '@command.IsRequired')"/>
}
Detail.cs html
@model ExampleModel
@Html.Partial("_Commands", Model)
ExampleController.cs
public ActionResult ExampleAction()
{
ExampleModel exampleModel = new ExampleModel()
exampleModel.Commands = new List<CommandModel>();
exampleModel.Commands.Add( new CommandModel() { Id = "1", IsRequired = false } );
exampleModel.Commands.Add( new CommandModel() { Id = "2", IsRequired = true } );
return View(exampleModel);
}
Сценарий:
- ExampleController's Метод ExampleAction вызывается.
- exampleModel построен правильно, который содержит заполненный список команд.
- _Commands.cs html страница отображается и получает ExampleModel.
- Когда страница _Command.cs html загружается, соответствующий foreach создает кнопки.
В результате я получил следующие строки: @command.IsRequired оценивается как OnClick . Почему?
<button type="button" onclick="command_onClick('1', 'onClick')"/>
<button type="button" onclick="command_onClick('2', 'onClick')"/>
Если я напишу так, то это хорошо.
<button type="button" onclick="command_onClick('@command.Id', '@command.IsRequired.ToString()')"/>
<button type="button" onclick="command_onClick('@command.Id', '@command.IsRequired.ToString()')"/>
Результат:
<button type="button" onclick="command_onClick('1', 'False')"/>
<button type="button" onclick="command_onClick('2', 'True')"/>
Откуда эта строка "onClick"? в случае печати без ToString ()?
Спасибо за ваши ответы.