Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NETC#4.0
Hello House,
Please i am new to MVC 4, i want to be able to capture what user enters on a form and store in a variable.
 
Also, i want to be able to display primary key field on view so that users can enter it manually. Thanks in advance.
Posted 4-Feb-13 22:52pm
Comments
JemiM at 5-Feb-13 5:27am
   
did you try anything?
mfon peter at 5-Feb-13 5:33am
   
Yes i tried this: [Key] [Required] [DatabaseGenerated(DatabaseGeneratedOption.none)] [ScaffoldColumn(true)] [Display(Name = "Email:")]. But still could not get the desired result i.e. being able to enter the key field manually.. i didn't even know how to start with the first question. i have carried out multiple searches!
JemiM at 5-Feb-13 5:29am
   
Getting started ASP.NET MVC http://www.asp.net/mvc
JemiM at 5-Feb-13 5:36am
   
Post your current Model,View and Controller that you have created
mfon peter at 5-Feb-13 5:51am
   
My Views: <fieldset> <div class="editor-label"> @Html.LabelFor(model => model.UserName) </div> <div class="editor-field"> @Html.EditorFor(model => model.UserName) @Html.ValidationMessageFor(model => model.UserName) </div> <div class="editor-label"> @Html.LabelFor(model => model.Password) </div> <div class="editor-field"> @Html.EditorFor(model => model.Password) @Html.ValidationMessageFor(model => model.Password) </div> <div class="editor-label"> @Html.LabelFor(model => model.ConfirmPassword) </div> <div class="editor-field"> @Html.EditorFor(model => model.ConfirmPassword) @Html.ValidationMessageFor(model => model.ConfirmPassword) </div> <p> <input type="submit" value="Create" /> </p> </fieldset> My Model: public class AccountInfoModel { [Key] [Required] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] [ScaffoldColumn(true)] [Display(Name = "Email:")] public string Eamil { get; set; } [Required] [Display(Name = "Prefered Username:")] public string UserName { get; set; } [Required] [Display(Name = "Password:")] [DataType(DataType.Password)] public string Password { get; set; } [Required] [Display(Name = "Confirm Password:")] [DataType(DataType.Password)] public string ConfirmPassword { get; set; } } My Controller: public ActionResult Create() { return View(); } // // POST: /AccountInfo/Create [HttpPost] public ActionResult Create(AccountInfoModel accountinfomodel) { if (ModelState.IsValid) { db.AccountInfos.Add(accountinfomodel); db.SaveChanges(); return RedirectToAction("Index"); } return View(accountinfomodel); } You can see, there's no email field on the view. the email field is the ID field.
JemiM at 5-Feb-13 5:37am
   
is Id field in the database is autoIncrement?
JemiM at 5-Feb-13 6:04am
   
You i didn't put the textBox for entering the id.I have create a sample application also i have put the solution for this.Please try.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Put Id field also in the markup like below also remove autoincrement property of the id field in the database.Put Id field in the model also.
<div class="editor-label">
                @Html.LabelFor(model => model.Id)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Id)
                @Html.ValidationMessageFor(model => model.Id)
            </div>
 
Hope this helps
  Permalink  
v2
Comments
mfon peter at 5-Feb-13 6:10am
   
Yes it really helps. thanks a lot. Please do you also know how to check for duplicate key, and how to capture user input from textboxes.
JemiM at 5-Feb-13 6:18am
   
before inserting take the data where Id= the id you have submitted to the server. if the record is null the id is not duplicated do the insert logic else id is duplicated print whatever the validation message.
JemiM at 5-Feb-13 6:20am
   
accessing user inputs i have mentioned in the first post.
JemiM at 5-Feb-13 6:29am
   
i have updated your view by adding the solution.
Marcus Kramer at 5-Feb-13 12:58pm
   
Please don't post multiple answers to one question. If you have updates to your answer, just use the Improve Solution widget and add/edit your answer. That way the train of thought is kept nicely in one place and makes the information easier to traverse.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Please go through the steps i have shared. This a sample application for posting the person details to the server.
 
Step1: Create a Controller by Right clicking the Controller like below
public class PersonController : Controller
   {
       //
       // GET: /Person/

       public ActionResult Index()
       {
           return View();
       }
 
       [HttpPost]
       public ActionResult Create(Person person)
       {
 
           try
           {
               // TODO: Add insert logic here
               string name = person.Name;
               return RedirectToAction("Index");
           }
           catch
           {
               return View();
           }
       }
 
   }
Step2 : Create a Model in the Model Folder like below
public class Person
   {
       [Key]
       public int Id { get; set; }
       public string Name { get; set; }
       public int Age { get; set; }
   }
 
Step3:Create a View by Right Clicking the Index Action that i have mentioned in the controller and put the mark up in that view by removing the existing markups
@model MvcApplication1.Models.Person
@{
    Layout = null;
}
 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    
    @using (Html.BeginForm("Create","Person")) {
        @Html.ValidationSummary(true)
    
        <fieldset>
            <legend>Person</legend>
            <div class="editor-label">
                @Html.LabelFor(model => model.Id)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Id)
                @Html.ValidationMessageFor(model => model.Id)
            </div>
            <div class="editor-label">
                @Html.LabelFor(model => model.Name)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Name)
                @Html.ValidationMessageFor(model => model.Name)
            </div>
    
            <div class="editor-label">
                @Html.LabelFor(model => model.Age)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Age)
                @Html.ValidationMessageFor(model => model.Age)
            </div>
    
            <p>
                <input type="submit" value="Create" />
            </p>
        </fieldset>
    }
</body>
</html>
.
Step4:Avoid the Autoincrement property of Id field from the Database
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Replace the existing view like below. Change the controllerName in the place of YourControllerName
@model AccountInfoModel
 
@{
    Layout = null;
}
 

 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    
    @using (Html.BeginForm("Create","YourControllerName")) {
        @Html.ValidationSummary(true)
    
       <fieldset>
        <legend>Create</legend>
        
        <div class="editor-label">
            @Html.LabelFor(model => model.UserName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.UserName)
            @Html.ValidationMessageFor(model => model.UserName)
        </div>
 
        <div class="editor-label">
            @Html.LabelFor(model => model.Password)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Password)
            @Html.ValidationMessageFor(model => model.Password)
        </div>
 
        <div class="editor-label">
            @Html.LabelFor(model => model.ConfirmPassword)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ConfirmPassword)
            @Html.ValidationMessageFor(model => model.ConfirmPassword)
        </div>
 
        
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
    }
</body>
</html>
  Permalink  
Comments
mfon peter at 5-Feb-13 7:01am
   
Please can you explain what this line does: @using (Html.BeginForm("Create","YourControllerName")), the one i have there are no parameters within the begin form method.
JemiM at 5-Feb-13 7:16am
   
post the data inside the beginForm to the server.
JemiM at 5-Feb-13 7:17am
   
Please see the videos in the ASP.NET mvc official site which explain detailed about how MVC works.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 710
1 OriginalGriff 543
2 Abhinav S 318
3 thatraja 245
4 Emre Ataseven 170
0 Sergey Alexandrovich Kryukov 8,322
1 OriginalGriff 4,790
2 Peter Leow 3,784
3 Maciej Los 3,515
4 Er. Puneet Goel 3,107


Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 5 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid