问题描述
这是我第一次使用MVC,因此如果我弄错了术语,请原谅.我正在研究PluralSight课程.
This is my first time with MVC so please excuse me if I am getting the terminology wrong. I am working on a PluralSight course.
我的理解是,在选择添加视图"时,支架会进入模型并创建一个视图.
My understanding is scaffolding goes to the model and creates a view when selecting Add View.
这是我的模特
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace OdeToFood.Models
{
public class RestaurantReview
{
public int Id { get; set; }
public int Rating { get; set; }
public string Body { get; set; }
public string ReviewerName { get; internal set; }
public int RestaurantId { get; set; }
}
}
我转到控制器操作,然后单击添加视图".构建视图时,我的视图缺少ReviewerName.我将ReviewerName手动添加到我的创建"视图中.将数据添加到数据库时,Model Binder无法识别ReviewerName.
I go to my controller action and click Add View. My view is missing ReviewerName when it is built. I add ReviewerName manually to my Create view. The ReviewerName appears to not be recognized by the Model Binder when data is added to the database.
我不确定从这里去哪里.谢谢您的帮助!
I am unsure where to go from here. Thanks for you help!
推荐答案
将属性设置器标记为内部时,脚手架将忽略它.只需将其从属性中删除,即可进行设置(双关语意):
When you mark the property setter as internal, the scaffolding ignores it. Simply remove that from the property and you should be set (pun intended):
public string ReviewerName { get; set; }
这篇关于ASP.NET MVC脚手架视图-缺少模型属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!