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

  • SEARCH
  • Home
  • 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 4553102
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T16:53:52+00:00 2026-05-21T16:53:52+00:00

I do it like this. It works but looks so ugly. And I don’t

  • 0

I do it like this. It works but looks so ugly. And I don’t have any clue to make it more meaningful.
I’m using .NET Framework 3.5 and MVC2

TRY TO Explain a little more…
With the following code, I set value to ‘temp’ again and again. if table ‘temp’ have 100 fields, then I have to set value 100 times. That’s what I mean ugly.

//
    // POST: /TableA/Create
    [HttpPost]
    public ActionResult Create(TableA formdata)
    {
        TableA temp = new TableA();

        //A foreign key model in another TableB
        var tbb = myDB.TableB.First(a => a.Id == formdata.TableB.Id);
        temp.TableB = tbb;

        //fields in this table
        temp.field1= formdata.field1;
        temp.field2= formdata.field2;
        temp.field3= formdata.field3;

        myDB.AddToTableA(temp);
        myDB.SaveChanges();
        return RedirectToAction("Index");
    }
  • 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-05-21T16:53:53+00:00Added an answer on May 21, 2026 at 4:53 pm

    Separating code in layers usually helps, because you don’t mix UI with business layer or data access layer… if that’s what you asked, because it’s rather hard to know what you’s like to change in your code.

    Otherwise your code looks as a mix of everything, becomes hard to maintain and understand what it does.

    Edit

    Instead of setting every property manually, you can do either:

    1. Use object initializers (C# 3.0 feature)

      TableA record = new TableA {
          TableB = myDB.TableB.First(a => a.Id == formdata.TableB.Id),
          Field1 = ... ,
          ...
      };
      
    2. Use static factory methods provided by entity classes:

      TableA record = TableA.CreateTableA(/* provide all values as method parameters */);
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create something that works like this but doesn't have any submenus.
I would like to sort this array but this code works if I do
This textcounter and limiter works great, but I'd like to be able to change
I have a text file which looks like this: blah blah foo1 bar1 foo1
Let's say I have a JSONObject in GWT that looks like this: {name1:value1, name2:value2}
This seems in my head like it should work but I cant figure out
I'm trying something like this, but this example does not work. jsObj = {};
Ideally I would like intellij to work with this but just getting them to
I really want to get this to work, but I feel like I've been
I would like to do this but it does not work. bool TryGetEnum<TEnum, TValue>(TValue

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.