sibonile
sibonile

Reputation: 109

how to solve this error on ID?

I'm using web API and MVC on my project. I've an error when I'm saving an id(PersonID) from a table(Person) to another table(Password). My problem is that when I'm trying to save PersonID to password table it saves zero instead of saving the auto generated PersonID on persons table.

Controller

   // POST: api/Person1
    [ResponseType(typeof(Person1))]
    public IHttpActionResult PostPerson1(UserVM vm)
    {
        var pers = new Person1
        {
            PersonID = vm.PersonID,
            Title = vm.Title,
            FirstName = vm.FirstName,
            LastName = vm.LastName,
         };

        var word = new Password();
        using (var context = new VybeEstoreEntities2())
        {
            context.People1.Add(pers);

            word.PersonID = pers.PersonID;

            word.Password1 = vm.Password1;

            context.Passwords.Add(word);
            context.SaveChanges();
        }

        return Ok(vm);
     }

UserVM

public class UserVM
{
    public int PersonID { get; set; }        
    public string FirstName { get; set; }       
    public string LastName { get; set; }      
    public string UserName { get; set; }       
    public string Password1 { get; set; }     
}

View

   <div class="control-group" ng-class="{error: form.PersonID.$invalid}">
        <label class="control-label">PersonID</label>
        <div class="controls">
            <input type="text" class="form-control" ng-model="lis.PersonID" id="PersonID" placeholder="" readonly="readonly">
        </div>
    </div>
     <div class="control-group" ng-class="{error: form.Password.$invalid}">
        <label class="control-label">Password</label>
        <div class="controls">
            <input type="text" class="form-control" ng-model="lis.Password1" id="Password1" placeholder="">
        </div>
    </div>

Upvotes: 1

Views: 59

Answers (1)

Patrick Hofman
Patrick Hofman

Reputation: 156938

You should fill the name of the input elements since that is used as unique key to send their values over the wire, not the ID. Also, I wonder if readonly makes them send too, maybe you need to make a hidden input, but that is just something you have to check yourself.

For example:

<input type="text" name="PersonID" class="form-control" ng-model="lis.PersonID" id="PersonID" placeholder="" readonly="readonly">

                   ^^^^

Upvotes: 1

Related Questions