Click here to Skip to main content
15,904,297 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I want to assign value of particular textbox from controller in view.
Here is my Controller Part Code


public ActionResult ShowUpdateView(int id)<br />
       {<br />
          var em =from p in GetPageBaseObject where p.EMP_ID==id select p;<br />
          ViewData["Emp"] = GetPageBaseObject;<br />
          ViewData["ee"] = em.ToList();<br />
          return View();<br />
        <br />
       }

Here GetpageBaseObject is the List


Now Here is my View Part Code



<script  runat="server"><br />
        protected void Page_Load(object sender, EventArgs e)<br />
        {  <br />
            ddl.DataSource = ViewData["Emp"];<br />
            ddl.DataBind();<br />
           <br />
        }<pre><pre lang="text"></pre></pre><br />
    </script>


Here Is my Textbox.Now how i assign my viewdata["ee"] which contains name of employee to textbox on pageload event.


<% using (Html.BeginForm())<br />
           { %><br />
        <%: Html.LabelFor(m => m.EMP_NAME) %><br />
        <%: Html.ValidationMessageFor(m => m.EMP_NAME)%><br />
        <%: Html.TextBoxFor(m => m.EMP_NAME, ViewData["ee"])%><br />
        <%}%>



Plz help me out.Whether i am doing it correctly or not.
Posted

1 solution

Please try below mentioned simple way.

<input type="text" name="EMP_NAME" value="<%: ViewData["ee"].ToString()%>">


UPDATE

With your query if it's returning only one result, why you use ToLits() with it.Please try is as below for the Text box.

C#
var em =from p in GetPageBaseObject where p.EMP_ID==id select p;

ViewData["ee"] = em.FirstOrDefault().EMP_NAME;
 
Share this answer
 
v3
Comments
Member 10435696 8-Feb-14 2:36am    
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
<input type="text" name="EMP_NAME" value=ViewData["ee"].ToString()>


ddl.DataSource = ViewData["Emp"];
ddl.DataBind();

}<pre><pre lang="text"></pre></pre>
</script>



It doesnt allow me to assign value in this code
Sampath Lokuge 8-Feb-14 2:42am    
Are you talking about the text box or the list ? What is the error ?
Sampath Lokuge 8-Feb-14 2:47am    
I have updated my solution also.Please check that.
Member 10435696 8-Feb-14 2:49am    
plz help me out..If i assign textbox this way in mvc
<%: Html.TextBoxFor(m => m.EMP_NAME, ViewData["ee"])%>

Then if i want to use id of textbox anywhere on viewpage...how can i do that..What is the id of textbox of above line?
Sampath Lokuge 8-Feb-14 2:52am    
You can check it by using chrome dev Tool.Just select above text box and then find the relevant id of that by using 'inspect element' on chrome dev tool.

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