Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
view page

XML
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function ()
    $("#Empmasters").change(function () {
        $("#Text1").val($("#Empmasters option:selected").text());
    });
});
</script>


And this is the .cshtml view code-

XML
<div class="editor-label">
@Html.LabelFor(model =&gt; model.Empid)
</div>
<div class="editor-field">
@*@Html.EditorFor(model => model.Empid)*@
//@Html.DropDownList("attemp", ViewBag.attemp as SelectList, "-Select Emp ID-", new { id="EmpID"
@Html.DropDownList("Empmasters", null, "--select----", new { @class = "dropdown" })
})
@Html.ValidationMessageFor(model => model.Empid)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Ename)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Ename, new {id="name"})
<input id="Text1" name="Text1" type="text" />
@Html.ValidationMessageFor(model => model.Ename)
</div>



action method-

C#
[HttpGet]
        public ActionResult show()
        {
            ViewBag.attemp = new SelectList(db.Empmasters, "Emp_Name", "Emp_Id");
            
            return View();
        }

[HttpPost]
public ActionResult show(Models.att att ,string Empmasters,  string Text1)
{
Empat ea = new Empat();
ea.AttID = att.Attid;
ea.Emp_Id = Convert.ToInt32(Empmasters);
ea.Emp_Name = Text1;
ea.Dept = att.dept;
ea.AttendMonth = att.attmonth;
ea.AttendDate = att.attdate;
ea.AttendanceType = att.atttype;
db.EmpAttends.InsertOnSubmit(ea);
db.SubmitChanges();
return view();
}


My question is when i insert data the value of Convert.ToInt32(Empmasters) is showing Name Not id.want to insert ID.It shows error.
Posted
Updated 14-Jan-16 23:06pm
v2
Comments
F-ES Sitecore 15-Jan-16 5:10am    
Google for how to bind dropdowns in MVC for examples of the proper usage of Html.DropDownList or Html.DropDownListFor.
Ujjval Shukla 15-Jan-16 5:15am    
I have tried many more.But my question is first of all i have populted data in dropdownlist from DB with ID and When is select ID the Related Name will display in TextBox.But when i insert data-
ea.Emp_Id = Convert.ToInt32(Empmasters);

the value of Empmasters showing the Name not ID.I know i have used-

ViewBag.attemp = new SelectList(db.Empmasters, "Emp_Name", "Emp_Id");

But is there any way to insert ID from dropdownlist with Name on textbox as well.
Ujjval Shukla 15-Jan-16 5:10am    
Thanks F-ES Sitecore for Updation.

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