简体   繁体   English

如何在asp.net MVC中自动生成的部分类中的View的iputs中设置属性的值

[英]How to set value of a property from iputs of View in auto generated partial class in asp.net MVC

I'm working in a ASP.NET MVC(EF6) database first project and I have an auto generated partial class in my Models folder like this: 我正在一个ASP.NET MVC(EF6)数据库第一个项目中工作,并且在Models文件夹中有一个自动生成的局部类,如下所示:

public partial class StdModel
{
    public string StdName { get; set; }
    public int StdNr { get; set; }
}

Because this is an auto generated class, every time I refresh my Models(for whatever reason) any change made to this class gets deleted, so I have extended this class and I would like to ser some input values that is passed from View to some properties(entities) in this new partial class, for example removing spaces between words. 因为这是一个自动生成的类,所以每当我刷新模型(无论出于何种原因)时,对该类所做的任何更改都会被删除,因此我扩展了该类,并希望将一些从View传递给某些输入值此新的局部类中的属性(实体),例如,删除单词之间的空格。

View: 视图:

<div class="form-group">
       @Html.LabelFor(model => model.StdName , htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.StdName , new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.StdName , "", new { @class = "text-danger" })
        </div>
</div>

However because it's an partial class I can't declare the same properties in this new partial class and I have already tried different approach like via constructor and set property with initializer to achieve this and yet no success, is there any way to set the properties and change if needed before it goes to db like this: 但是,因为它是一个局部类,所以我不能在这个新的局部类中声明相同的属性,而且我已经尝试了不同的方法,例如通过构造函数和带有初始化器的set属性来实现此目的,但没有成功,有没有办法设置属性并在进入数据库之前按需要进行更改,如下所示:

public partial class StdModel   <---  PartialClasses.cs 
{
    private string stdName;
    public string StdName 
    { 
      get { return stdName; }
      set { stdName = value.ToUpper(); }
    }
    private int stdNr;
    public int StdNr 
    { 
      get { return stdNr; }
      set {stdNr = Regex.Replace(value, @"\s+", ""); }
    }
}

Thanks in advance. 提前致谢。

A possible, potentially acceptable workaround may be to explicitly implement an interface. 一个可能的潜在可接受的解决方法可能是显式实现接口。 Example: 例:

using System;
using System.Text.RegularExpressions;

namespace StackOverflow_AutoPartialPropOverriding
{
    class Program
    {
        static void Main(string[] args)
        {
            IStdModel model = new StdModel();
            Console.WriteLine($"StdName: {model.StdName}");
            Console.WriteLine($"StdNr: {model.StdNr}");
            /* StdName: steve
             * StdNr: 1
             */
            Console.ReadKey();
        }
    }

    public partial class StdModel
    {
        public string StdName { get; set; }
        public int StdNr { get; set; }
    }

    public interface IStdModel
    {
        string StdName { get; set; }
        int StdNr { get; set; }
    }

    public partial class StdModel : IStdModel
    {
        private string stdName = "steve";
        string IStdModel.StdName
        {
            get => stdName;
            set => value.ToUpper();
        }

        private int stdNr = 1;
        int IStdModel.StdNr
        {
            get => stdNr;
            set => stdNr = value; //value is already type int--not sure why you used regex
        }
    }
}

Notes: 笔记:

It is widely accepted that setting a property should be a very simple operation. 设置属性应该是非常简单的操作,这一点已被广泛接受。 Changing values in setters makes properties return unexpected values. 在setter中更改值会使属性返回意外值。 You could still do this --aside from the partial class problem-- if you think it's really inevitable, but note that EF will also use the setter when materializing StdModel objects. 如果您认为这确实是不可避免的,那么除了部分类问题之外,您仍然可以执行此操作,但是请注意,EF在实现StdModel对象时也会使用setter。 If there is a deviant value in the database you won't notice! 如果数据库中存在异常值,您将不会注意到!

Taking all this into account I would either create two delegate properties that convert the value to the mapped property, for example ... 考虑到所有这些,我将创建两个委托属性,将值转换为映射的属性,例如...

public string StdNameSetToUpper
{ 
  get { return StdName; }
  set { StdName = value.ToUpper(); }
}

... or (preferably) work with a view model that transfers converted values between the UI and the data layer. ...或(最好)与视图模型一起使用,该视图模型在UI和数据层之间传输转换后的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM