textbox and dropdown is not  accepting the value in mvc

sai krishnan
sai krishnan used Ask the Experts™
on
hi experts,
There is a text box for accepting phoneno and gender using dropdown. when i full the form with phone number and gender it is not accepting the entered value in textbox or dropdown for inserting.can anyone help me on this

in sql,it is inserting as 0 and dropdown as null

this is in cshtml
 <tr>
                <td>
                    @Html.LabelFor(a=>a.Phoneno)
                </td>
            </tr>

            <tr>
                <td>
                    @Html.TextBoxFor(a => a.Phoneno)
                    @Html.ValidationMessageFor(a => a.Phoneno)






                </td>

            </tr>
            <tr>
                <td>
                    @Html.Label("Gender")
                </td>
            </tr>

            <tr>
                <td>
                    @Html.DropDownListFor(a => a.gender, new List<SelectListItem>{

                    new SelectListItem {Text="Male",Value="0" },
                                        new SelectListItem {Text="Female",Value="1" }
              })
                    @Html.ValidationMessageFor(a => a.gender)


                </td>

in entity framework

 public int Phoneno { get; set; }
        public string gender { get; set; }


in controller
  mydbs itr = new mydbs();
            string result = itr.insertcmd(reg);
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
IT Professional
Top Expert 2015
Commented:
while submitting, call jQuery validation function like
$('form').submit(function(evt) {...
    var $form = $(this);
    if($form.valid()) {
        return true;
    }
return false;
});

Open in new window


In controller, you can check with
if (ModelState.IsValid)
{
//DO DB operations
}

Open in new window

Hope above solution helps you!
If not, please provide more relevant part code to check with (hard to understand from current block).
sai krishnanSoftware Developer

Author

Commented:
thank you
if I add model.isvalid ,it always return false.
Prakash SamariyaIT Professional
Top Expert 2015

Commented:
If not, please provide more relevant part code to check with (hard to understand from current block).
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

sai krishnanSoftware Developer

Author

Commented:
hi sir,


i have form with field first name,last name,email,phone and gender etc
Here phone is a text box which accept integer value.my problem is in this text box. It is not accepting the given phone number value instead it is inserting only null value.but other values are varchar value,it got inserted. i am using sql 2016 database

this is the cshtml of phoneno


 <tr>
                <td>
                    @Html.LabelFor(a=>a.Phoneno)
                </td>
            </tr>
            <tr>
                <td>
                    @Html.TextBoxFor(a=>a.Phoneno)

                </td>

            </tr>

i have created separate class library project.this class library project as edmx .model value of phoneno is coming from this edmx.
model cs of edmx

public int Phoneno { get; set; }

controller cs for passing values

 mydbs itr = new mydbs(); -this is class library project
                string result = itr.insertcmd(reg); -this is the method in that class file
                ViewData["result"] = result;


code in class library


 public string insertcmd(registration reg)
        {
           using (customerEntities con = new customerEntities())
            {

                string result = con.registrations.Add(reg).ToString();
                con.SaveChanges();
                return result;




            }

        }

    }
Prakash SamariyaIT Professional
Top Expert 2015
Commented:
You need to check in controller method whether your model gets all values or not by posting.

Then you need to check for validation by
if (ModelState.IsValid)
{
//DO DB operations
}

Open in new window

Remember it is ModelState.IsValid and not Model.IsValid
sai krishnanSoftware Developer

Author

Commented:
thank you .i will try and let you know sir

thank you once again

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial