简体   繁体   中英

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:

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:

<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:

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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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