I have written a Model, Controller and an ASP Page(view)
My page is very simple that it connects to an MS -Access database and fetch the data and show it on the page when i click the Load push button.
Here is my code//
<div>Model Code...</div> <div>*****************</div> <div>using System.Data.SqlClient;</div> <div>using
System.Data.OleDb;</div> <div>namespace MvcApplication1.Models</div> <div>{</div> <div>public
class
ss
</div> <div>{</div> <div>public
int PID {
get;
set; }</div> <div>public
string FName {
get;
set; }</div> <div>public
string LName {
get;
set; }</div> <div>public
string Email {
get;
set; }</div> <div>public
Double Phone {
get;
set; }</div> <div> </div> <div>public
ss()</div> <div>{</div> <div>}</div> <div>public ss(int
pid, String fname,
String lname,
String email,
Double phone)</div> <div>{</div> <div>this.PID
= pid;</div> <div>this.FName = fname;</div> <div>this.LName
= lname;</div> <div>this.Email = email;</div> <div>this.Phone
= phone;</div> <div>}</div> <div>}</div> <div>}</div> <div> </div> <div>=======================================================================</div>
<div>Controller Code...</div> <div>********************</div> <div>using System.Web.Mvc.Ajax;</div>
<div>using MvcApplication1.Models;</div> <div>using
System.Data.OleDb;</div> <div>namespace MvcApplication1.Controllers</div> <div>{</div>
<div>[HandleError]</div> <div>public
class
ssController :
Controller</div> <div>{</div> <div>public
ActionResult ss()</div> <div>{</div> <div>return
View();</div> <div>}</div> <div>public
ActionResult load1()</div> <div>{</div> <div>OleDbConnection
mycon;</div> <div>OleDbCommand com;</div> <div>ss
usr = new
ss();</div> <div>mycon =
new
OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Vijay\Desktop\Proof
Concept\Person.mdb;Persist Security Info=False");</div> <div>mycon.Open();</div> <div>com =
new
OleDbCommand("select * from person", mycon);</div>
<div>OleDbDataReader dr = com.ExecuteReader();</div> <div>if
(dr.Read())</div> <div>{</div> <div>usr.PID = dr.GetInt32(0);</div> <div>usr.FName = dr.GetString(1);</div> <div>usr.LName
= dr.GetString(2);</div> <div>usr.Email = dr.GetString(3);</div> <div>usr.Phone = dr.GetDouble(4);</div> <div>}</div>
<div>mycon.Close();</div> <div>ViewData.Model = usr;</div> <div>return
View("ss");</div> <div>}</div> <div>}</div> <div>
<div>=======================================================================</div> <div> ASP Page Code...</div> <div>***********************</div> <div><%@
Page
Title="PERSON SS Test"
Language="C#"
MasterPageFile="~/Views/Shared/Site_NoMenu.Master"
Inherits="System.Web.Mvc.ViewPage" %></div> <div><script
runat="server"></div> <div>protected
void Button1_Click(object sender,
EventArgs e)</div> <div>{</div> <div>form1.Action =
"/ss/load1";</div> <div>PID.Text = usr.PID; // Not Working</div> <div>}</div> <div> <div><table></div>
<div><tr></div> <div><td
align="left"
valign="top"
style="width:10%"></div>
<div><asp:Label
ID="Label2"
runat="server"
Text="Person Id:"
font-Bold="true"
Font-Size="X-Small"></asp:Label></div>
<div></td></div> <div><td
align="left"
valign="top"
style="width:90%"></div>
<div><asp:TextBox
ID="PID"
runat="server"></asp:TextBox></div>
<div></td></div> <div></tr></div>
<div><tr></div> <div><td></div>
<div><asp:Button
ID="Button1"
Text="Load"
runat="server"
onclick="Button1_Click"
/> </div> <div></td></div>
<div></tr></div> <div></table></div>
<div>================================================================================================================================</div> <div> </div> <div>I dont know to bring the data back from the controller to my Page for the Text Box
PID.TEXT = </div> <div>Although if i use
the following
<%= Html.TextBox("PID")%> // it works.
<div><asp:TextBox
ID="PID"
runat="server"></asp:TextBox>
// not working...</div> <div>Can any one help me..</div> <div>Thanks in advance...</div> <div> </div> <div>R,</div> <div>VJ</div>
I'm pretty sure that the problem is because you're trying to mix ASP.NET and ASP.NET MVC Code. If you go with ASP.NET MVC, just know that the <asp:*> controls won't work.You also may want to run through some of the tutorials at http://www.asp.net/learn/mvc/
.
It is possible to create hybrid applications that use both technologies, but I wouldn't recommend it for someone who's just starting out.
I have started learning ASP in less than a month and now I have already designed 80 web pages in my project with <ASP:Textbox> etc. and dont want to use <% Html > tags, definetly i want to use these <ASP:textbox> in my code. Iam going to use rich formating
and validations using these <ASP: > . Please let me know how to get the value from the controller on to my push button event and display in my web page textbox with out modifying the <asp:*>. ??
VJ, Sorry to hear about so many pages, perhaps you're better off converting the whole hting to a regular web forms app?
While you *should* be able to get ahold of the Model property in the ViewPage and set a value from that equal to the text of a textbox, you're probably going to cause other problems elsewhere in yoru app, since textboxes and the like are built to use viewstate,
while the MVC framework does not provide viewstate, and things like postbacks and click events are alien to the MVC framework's infrastructure.
Help those who have helped you... remember to "Mark as Answered"
I have already designed 80 web pages in my project with <ASP:Textbox> etc. and dont want to use <% Html > tags, definetly i want to use these <ASP:textbox> in my code. Iam going to use rich formating and validations using these <ASP: > .
Then ASP.NET MVC probably isn't for you. Why not stick with the existing WebForms-based code if it seems to be working?
My project manager is specific about coding in MVC and ASP.net (C#). Initially i didnt understand the model, but now im getting bit hold on this. If you can help me in getting this solved it will be of gr8 help.
My project manager is specific about coding in MVC and ASP.net (C#).
If your project manager is asking you to use both ASP.NET MVC and the standard ASP.NET built-in controls, then he probably doesn't have a good grasp of what ASP.NET MVC is. These two things are fundamentally incompatible.
Honestly, there's not an easy solution to your problem. 80 pages or no, your best bet is to learn the way the MVC framework is done right (if he's insisting on it) and then do it that way. Anything else will be a lot more work and probably not meet your
project manager's goals.
Help those who have helped you... remember to "Mark as Answered"
ton_vj
0 Points
8 Posts
C# MVC Textbox contol - Help
Apr 21, 2009 06:14 PM|LINK
Can any one help me??
I am new to ASP and ASP.net (MVC).
I have written a Model, Controller and an ASP Page(view)
My page is very simple that it connects to an MS -Access database and fetch the data and show it on the page when i click the Load push button.
Here is my code//
<div>Model Code...</div> <div>*****************</div> <div>using System.Data.SqlClient;</div> <div>using System.Data.OleDb;</div> <div>namespace MvcApplication1.Models</div> <div>{</div> <div>public class ss </div> <div>{</div> <div>public int PID { get; set; }</div> <div>public string FName { get; set; }</div> <div>public string LName { get; set; }</div> <div>public string Email { get; set; }</div> <div>public Double Phone { get; set; }</div> <div> </div> <div>public ss()</div> <div>{</div> <div>}</div> <div>public ss(int pid, String fname, String lname, String email, Double phone)</div> <div>{</div> <div>this.PID = pid;</div> <div>this.FName = fname;</div> <div>this.LName = lname;</div> <div>this.Email = email;</div> <div>this.Phone = phone;</div> <div>}</div> <div>}</div> <div>}</div> <div> </div> <div>=======================================================================</div> <div>Controller Code...</div> <div>********************</div> <div>using System.Web.Mvc.Ajax;</div> <div>using MvcApplication1.Models;</div> <div>using System.Data.OleDb;</div> <div>namespace MvcApplication1.Controllers</div> <div>{</div> <div>[HandleError]</div> <div>public class ssController : Controller</div> <div>{</div> <div>public ActionResult ss()</div> <div>{</div> <div>return View();</div> <div>}</div> <div>public ActionResult load1()</div> <div>{</div> <div>OleDbConnection mycon;</div> <div>OleDbCommand com;</div> <div>ss usr = new ss();</div> <div>mycon = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Vijay\Desktop\Proof Concept\Person.mdb;Persist Security Info=False");</div> <div>mycon.Open();</div> <div>com = new OleDbCommand("select * from person", mycon);</div> <div>OleDbDataReader dr = com.ExecuteReader();</div> <div>if (dr.Read())</div> <div>{</div> <div>usr.PID = dr.GetInt32(0);</div> <div>usr.FName = dr.GetString(1);</div> <div>usr.LName = dr.GetString(2);</div> <div>usr.Email = dr.GetString(3);</div> <div>usr.Phone = dr.GetDouble(4);</div> <div>}</div> <div>mycon.Close();</div> <div>ViewData.Model = usr;</div> <div>return View("ss");</div> <div>}</div> <div>}</div> <div> <div>=======================================================================</div> <div> ASP Page Code...</div> <div>***********************</div> <div><%@ Page Title="PERSON SS Test" Language="C#" MasterPageFile="~/Views/Shared/Site_NoMenu.Master" Inherits="System.Web.Mvc.ViewPage" %></div> <div><script runat="server"></div> <div>protected void Button1_Click(object sender, EventArgs e)</div> <div>{</div> <div>form1.Action = "/ss/load1";</div> <div>PID.Text = usr.PID; // Not Working</div> <div>}</div> <div> <div><table></div> <div><tr></div> <div><td align="left" valign="top" style="width:10%"></div> <div><asp:Label ID="Label2" runat="server" Text="Person Id:" font-Bold="true" Font-Size="X-Small"></asp:Label></div> <div></td></div> <div><td align="left" valign="top" style="width:90%"></div> <div><asp:TextBox ID="PID" runat="server"></asp:TextBox></div> <div></td></div> <div></tr></div> <div><tr></div> <div><td></div> <div><asp:Button ID="Button1" Text="Load" runat="server" onclick="Button1_Click" /> </div> <div></td></div> <div></tr></div> <div></table></div> <div>================================================================================================================================</div> <div> </div> <div>I dont know to bring the data back from the controller to my Page for the Text Box PID.TEXT = </div> <div>Although if i use the following<%= Html.TextBox("PID")%> // it works.
<div><asp:TextBox ID="PID" runat="server"></asp:TextBox> // not working...</div> <div>Can any one help me..</div> <div>Thanks in advance...</div> <div> </div> <div>R,</div> <div>VJ</div>
</div></div></div>MVC C# Textbox MS-Access
VJ
pmourfield
Participant
946 Points
155 Posts
Re: C# MVC Textbox contol - Help
Apr 21, 2009 07:50 PM|LINK
I'm pretty sure that the problem is because you're trying to mix ASP.NET and ASP.NET MVC Code. If you go with ASP.NET MVC, just know that the <asp:*> controls won't work.You also may want to run through some of the tutorials at http://www.asp.net/learn/mvc/ .
It is possible to create hybrid applications that use both technologies, but I wouldn't recommend it for someone who's just starting out.
ton_vj
0 Points
8 Posts
Re: C# MVC Textbox contol - Help
Apr 21, 2009 08:15 PM|LINK
Thanks Peter,
I have started learning ASP in less than a month and now I have already designed 80 web pages in my project with <ASP:Textbox> etc. and dont want to use <% Html > tags, definetly i want to use these <ASP:textbox> in my code. Iam going to use rich formating and validations using these <ASP: > . Please let me know how to get the value from the controller on to my push button event and display in my web page textbox with out modifying the <asp:*>. ??
Thanks for the Help
R,
VJ
C# MVC Textbox contol - Help
VJ
paul.vencill
Contributor
6716 Points
1358 Posts
Re: C# MVC Textbox contol - Help
Apr 21, 2009 08:27 PM|LINK
VJ, Sorry to hear about so many pages, perhaps you're better off converting the whole hting to a regular web forms app?
While you *should* be able to get ahold of the Model property in the ViewPage and set a value from that equal to the text of a textbox, you're probably going to cause other problems elsewhere in yoru app, since textboxes and the like are built to use viewstate, while the MVC framework does not provide viewstate, and things like postbacks and click events are alien to the MVC framework's infrastructure.
levib
Star
7702 Points
1099 Posts
Microsoft
Re: C# MVC Textbox contol - Help
Apr 21, 2009 08:30 PM|LINK
Then ASP.NET MVC probably isn't for you. Why not stick with the existing WebForms-based code if it seems to be working?
ton_vj
0 Points
8 Posts
Re: C# MVC Textbox contol - Help
Apr 21, 2009 08:36 PM|LINK
Peter,
Thanks.. Can you show a sample piece of code for this... For to have better understanding..
R,
VJ
MVC C# MS-Access <asp:Textbox
VJ
ton_vj
0 Points
8 Posts
Re: C# MVC Textbox contol - Help
Apr 21, 2009 08:39 PM|LINK
Hi
My project manager is specific about coding in MVC and ASP.net (C#). Initially i didnt understand the model, but now im getting bit hold on this. If you can help me in getting this solved it will be of gr8 help.
Thanks
VJ
C# MVC Textbox contol - Help
VJ
levib
Star
7702 Points
1099 Posts
Microsoft
Re: C# MVC Textbox contol - Help
Apr 21, 2009 08:49 PM|LINK
If your project manager is asking you to use both ASP.NET MVC and the standard ASP.NET built-in controls, then he probably doesn't have a good grasp of what ASP.NET MVC is. These two things are fundamentally incompatible.
If you really, really need to use MVC and some type of server-side controls, you can find some experimental ones in the MVC Futures assembly on CodePlex. But they're nowhere near as powerful as it sounds like you need, e.g. they don't have built-in features like validation. See http://weblogs.asp.net/leftslipper/archive/2009/03/03/asp-net-mvc-release-candidate-2-i-declare-myself-to-be-declarative.aspx for more information.
paul.vencill
Contributor
6716 Points
1358 Posts
Re: C# MVC Textbox contol - Help
Apr 21, 2009 08:50 PM|LINK
Honestly, there's not an easy solution to your problem. 80 pages or no, your best bet is to learn the way the MVC framework is done right (if he's insisting on it) and then do it that way. Anything else will be a lot more work and probably not meet your project manager's goals.
ton_vj
0 Points
8 Posts
Re: C# MVC Textbox contol - Help
Apr 21, 2009 09:02 PM|LINK
Paul,
Ok,, How to call the function() that is there in the controller from my button click event../
VJ