Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8693437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:32:35+00:00 2026-06-13T00:32:35+00:00

Customers.cs public partial class Customers { public int sno { get; set; } public

  • 0

Customers.cs

public partial class Customers
{
    public int sno { get; set; }
    public string CustomerName { get; set; }
    public string CustomerNo { get; set; }
    ...
    // 20 more attribute too...
}

Cities.cs

public partial class Cities
{
    public int sno { get; set; }
    public string CityName { get; set; }
    public string CityPlate { get; set; }
    public string CityPhoneCode { get; set; }
}

AddCustomerViewModel.cs

public class AddCustomerViewModel
{

    [Required(ErrorMessage = "Şehir seçiniz.")]
    [Display(Name = "Şehir")]
    public Nullable<int> CityId { get; set; }

    // same with Customers.cs
    public int sno { get; set; }

    [Required(ErrorMessage = "Müşteri adını giriniz!")]
    [Display(Name = "Müşteri Adı")]
    public string CustomerName { get; set; }

    [Required(ErrorMessage = "Müşteri numarası giriniz!")]
    [Display(Name = "Müşteri Numarası")]
    public string CustomerNo { get; set; }
    ...
    // 20 more attribute too...
}

Controller

[Authorize(Roles = "Administrator")]
public ActionResult AddCustomer()
{
    AddCustomerViewModel addCustomerViewModel = new AddCustomerViewModel();
    addCustomerViewModel.Cities = entity.Cities;

    return View(addCustomerViewModel);
}

[HttpPost]
[Authorize(Roles = "Administrator")]
public ActionResult AddCustomer(AddCustomerViewModel addCustomerViewModel)
{
    entity.Customers.Add(GetCustomerFromViewModel(addCustomerViewModel));
    entity.SaveChanges();

    return View(addCustomerViewModel);
}

I m using a function that is called GetCustomerFromViewModel to convert addCustomerViewModel to Customer like below:

GetCustomerFromViewModel()

private Customers GetCustomerFromViewModel(AddCustomerViewModel addCustomerViewModel)
{
    Customers customer = new Customers();

    customer.CityId = addCustomerViewModel.CityId;
    customer.CreatorUserId = (Guid)System.Web.Security.Membership.GetUser().ProviderUserKey;
    customer.CustomerName = addCustomerViewModel.CustomerName;
    customer.CustomerNo = addCustomerViewModel.CustomerNo;
    customer.Description = addCustomerViewModel.Description;
    ...
    // 20 more attribute too...

    return customer;
}

But Customers class have too many variable (customerNo, CustomerName, …) , So this is the not good way.

When I use DbContextGenerator and Add classes to dataAnnotations and then When I udated the model, dataAnnotations is deleted. (Because DbContext classes are updated, too)

How Can I use ViewModels with DataAnnotations. And effective insert operation to Db? Article, Tutorial, example or advice?

I hope I can explain.

Thanks a lot…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T00:32:36+00:00Added an answer on June 13, 2026 at 12:32 am

    You may take a look at AutoMapper which will simplify the mapping logic between your domain models and view models so that you don’t need to manually map each property. Other than that there’s nothing wrong with your code. You are already using a view model and have a mapping layer. So your GetCustomerFromViewModel function might become:

    private Customers GetCustomerFromViewModel(AddCustomerViewModel addCustomerViewModel)
    {
        return Mapper.Map<AddCustomerViewModel, Customers>(addCustomerViewModel);
    }
    

    or completely get rid of it and directly use the AutoMapper call in your controller action because this function no longer brings much value:

    [HttpPost]
    [Authorize(Roles = "Administrator")]
    public ActionResult AddCustomer(AddCustomerViewModel addCustomerViewModel)
    {
        var customer = Mapper.Map<AddCustomerViewModel, Customers>(addCustomerViewModel);
        entity.Customers.Add(customer);
        entity.SaveChanges();
    
        return View(addCustomerViewModel);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following VB.NET class definition: <Serializable()> Partial Public Class Customers End Class
Suppose I have the view models below. public class CustomerListViewModel { public IEnumerable<CustomerViewModel> Customers
Here is one of my Entityclasses: public class Customer { public int Id {
Say you have two classes, order and customer: public class Customer{ public int CustomerId
I have the following code: class Customers : BindableObject { private ObservableCollection<string> _Products =
Here is my C# code for Mainform.cs public partial class MainForm : Form {
I have a Customer class. public class Customer { private string _id; private string
I have a Generic List as below public static readonly List<Customer> Customers = new
I need use this method with three distinct classes: Orders, Customers, Suppliers public void
Razor: @Html.TextBoxFor(kod => kod.Name) @Html.ValidationMessage(Name,Client Error Message) Controller: [HttpPost] public JsonResult JsonAddCustomer(Customers customer, string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.