Asp.net mvc MVC如何使用默认值创建新对象

Asp.net mvc MVC如何使用默认值创建新对象,asp.net-mvc,razor,Asp.net Mvc,Razor,我有一个视图,有添加/编辑,编辑工作正常,但添加我想为类型设置默认值。在cshtml文件的视图中是否有这样做的方法 添加视图 @Html.Partial("RegimenReferences", new (ReferencesModel {Type = "defaultType}") ) 编辑视图 @Html.Partial("RegimenReferences", (ReferencesModel)Model) 模型 您想在cshtml中专门设置这些类型吗 您能否为您的模型创建一个新的构造

我有一个视图,有添加/编辑,编辑工作正常,但添加我想为类型设置默认值。在cshtml文件的视图中是否有这样做的方法

添加视图

@Html.Partial("RegimenReferences", new (ReferencesModel {Type = "defaultType}") )
编辑视图

@Html.Partial("RegimenReferences", (ReferencesModel)Model)
模型


您想在cshtml中专门设置这些类型吗

您能否为您的模型创建一个新的构造函数,该构造函数接受您想要设置为默认值的任何字段

public class ReferencesModel
    {
        public ReferencesModel(string type = null)
        {
            Type = type;
        }

        public ReferencesModel(Reference reference)
        {
            this.Id = reference.Id;
            this.Link = reference.Link;
            this.Text = reference.Text;
            this.Type = reference.Type;
            this.Regimens = reference.Regimens;
            this.GuidelineId = reference.GuidelineId;
            this.SortOrder = reference.SortOrder;
        }

        public long Id { get; set; }
        public string Link { get; set; }
        public string Text { get; set; }
        public string Type { get; set; }
        public int Regimens { get; set; }
        public Guid? GuidelineId { get; set; }
        public int SortOrder { get; set; }
    }
或者只是在构造函数/变量声明中设置一个默认值

public ReferencesModel()
    {
        Type = "default type";
    }

public string Type = "default type";

我想在cshtmlAt中设置调用部分的值?@Html.Partial(“RegimenReferences”,new ReferencesModel{Type=“defaultType”})不起作用吗?或者使用构造函数执行@Html.Partial(“RegimenReferences”,新的ReferencesModel(“default type”))注意:
public string type=“default type”定义公共字段。要使用默认值设置公共属性,需要使用
公共字符串类型{get;set;}=“default Type”。否则,如果您选择将其设置为完整属性,您将在以后对公共接口引入突破性的更改。此外,您应该在构造函数中定义默认值或将其定义为属性的默认值。没有必要两者都做。
public ReferencesModel()
    {
        Type = "default type";
    }

public string Type = "default type";