textbox and dropdown is not accepting the value in mvc

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);
sai krishnanSoftware DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Prakash SamariyaIT ProfessionalCommented:
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).
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sai krishnanSoftware DeveloperAuthor Commented:
thank you
if I add model.isvalid ,it always return false.
0
Prakash SamariyaIT ProfessionalCommented:
If not, please provide more relevant part code to check with (hard to understand from current block).
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

sai krishnanSoftware DeveloperAuthor 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;




            }

        }

    }
0
Prakash SamariyaIT ProfessionalCommented:
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
0
sai krishnanSoftware DeveloperAuthor Commented:
thank you .i will try and let you know sir

thank you once again
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET MVC

From novice to tech pro — start learning today.