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 9299109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:29:01+00:00 2026-06-18T22:29:01+00:00

I am having trouble writing the code for saving a text field box information

  • 0

I am having trouble writing the code for saving a text field box information into the database and this is what I have – a Get and Post method:

public ActionResult _UserName(UserNameModel userNameModel)
{
    using (var db = new DataConnection())
    {
         userNameModel.UserName= (from u in db.Users
                                 where u.ID == WebSecurity.CurrentUserId
                                 select u.UserName).FirstOrDefault();
         }
         return View(userNameModel);
    }
}   


[HttpPost]
public ActionResult _CreateUserName(string username, UserNameModel userNameModel)
{
    if (ModelState.IsValid)
    {
        using (var db = new DataConnection())
        {
            try
            {
                // Need to save what has been entered in the textbox 
                // to the Users table in the database. 
            }
            catch (Exception)
            {
                throw;
            }
        }
        return RedirectToAction("UserNamePage");
    }
    return View(userNameModel);
}
  • 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-18T22:29:02+00:00Added an answer on June 18, 2026 at 10:29 pm

    You do not need to use FormCollection here as it appears as if you are binding to a model.

    In the View you should have

    @model MySystem.Models.UserNameModel 
    
    @Html.TextBoxFor(m=> m.Username)
    

    Then when you submit the form you can get the model data from userNameModel.Username;

    Your Post method only needs to collect the model data

    [HttpPost]
    public ActionResult _CreateUserName(UserNameModel userNameModel)
    {
        if (ModelState.IsValid)
        {
          using (var db = new DataConnection())
          {
            try
            {
                db.UsersTable.InsertOnSubmit(new User(){ Username = userNameModel});
                // Need to save what has been entered in the textbox 
                // to the Users table in the database. 
            }
            catch (Exception)
            {
                throw;
            }
        }
        return RedirectToAction("UserNamePage");
    }
    return View(userNameModel);
    
    
    
    }
    

    Edit My preferred method for interacting with the database is to use Linq, in your project add a new item > Data > LinqToSql. This will give you a diagrammatic overview and you can simply drag tables and procedures to the screen. This will create a model of each table.

    You can then create an instance of the Linq context and use this to update your database, you will see intellisense will pick up the tables in your designer. You will then be able to interact with the database very cleanly

    [HttpPost]
    public ActionResult _CreateUserName(UserNameModel userNameModel)
    {
        if (ModelState.IsValid)
        {
    
            try
            {
                myLinqDataContext db = new myLinqDatacontext(); // in your web config amend the connection string this created
    
                db.UsersTable.InsertOnSubmit(new User(){ Username = userNameModel});
                // Need to save what has been entered in the textbox 
                // to the Users table in the database. 
            }
            catch (Exception)
            {
                throw;
            }
    
        return RedirectToAction("UserNamePage");
    }
    return View(userNameModel);
    
    
    
    }
    

    If you want to Update you will need to call an Instance of the record in the database

    [HttpPost]
    public ActionResult _CreateUserName(UserNameModel userNameModel)
    {
        if (ModelState.IsValid)
        {
    
            try
            {
                myLinqDataContext db = new myLinqDatacontext(); // in your web config amend the connection string this created
    
                UserTable user = db.UsersTable.Where(m=> m.Username == userNameModel.UserName).FirstOrDefault();
    
                // apply new information
                user.username = userNameModel.UserName;
    
                // commit the changes
                db.SubmitChanges();
    
                // Need to save what has been entered in the textbox 
                // to the Users table in the database. 
            }
            catch (Exception)
            {
                throw;
            }
    
        return RedirectToAction("UserNamePage");
    }
    return View(userNameModel);
    
    
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm quite new to sql and I'm having trouble writing this i have a
Im having some trouble writing a getstring function, this is what I have so
Well, I'm having trouble with this code, it's about writing the Selection Sort alghorithm
I'm having trouble writing maintainable CSS code when font size is specified with EM
I am having trouble writing a query. I have been working with UNION on
I am having trouble writing C++ code that uses a header file designed for
I am beginning to delve deeper into Perl, but am having trouble writing Perl-ly
Im having trouble with writing coloumn names using a FlexTable in GWT my code
I'm new to writing assembly code and I'm having trouble printing out the values
I'm having some trouble writing a query using variables. Here's my code Dim bondnumber

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.