Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Comments
Jameel VM 5-Feb-13 5:27am    
did you try anything?
Uwakpeter 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!
Jameel VM 5-Feb-13 5:29am    
Getting started ASP.NET MVC http://www.asp.net/mvc
Jameel VM 5-Feb-13 5:36am    
Post your current Model,View and Controller that you have created
Uwakpeter 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.

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.
HTML
<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
 
Share this answer
 
v2
Comments
Uwakpeter 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.
Jameel VM 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.
Jameel VM 5-Feb-13 6:20am    
accessing user inputs i have mentioned in the first post.
Jameel VM 5-Feb-13 6:29am    
i have updated your view by adding the solution.
fjdiewornncalwe 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.
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
C#
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
C#
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
C#
@model MvcApplication1.Models.Person
@{
    Layout = null;
}

HTML
<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
 
Share this answer
 
Replace the existing view like below. Change the controllerName in the place of YourControllerName
HTML
@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>
 
Share this answer
 
Comments
Uwakpeter 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.
Jameel VM 5-Feb-13 7:16am    
post the data inside the beginForm to the server.
Jameel VM 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900