asp.net mvc using div or any such text control - asp.net

I have been trying to use div element in the view in controller but i can't find the control.
This is what I've done in the view:
<div id="z" style="display:none">
<p id="af">Please enter your CustomerID.You cannot try to enter Products for a different Customer </p>
</div>
controller side:
The intellisense does not show me div id. and Findcontrol seems to be useless. So, I need to make the div element viewable. Can you please help me?

I think you have major misunderstandings about how ASP.NET MVC works. There are no server controls. There is no ViewState, PostBacks and code behind contrary to what you might have the habit of using in classic WebForms. In ASP.NET MVC you write a Controller action which will receive the user request, manipulate the Model and pass a view model to the View for rendering.
I would recommend you checking out the following site and familiarize yourself with the basic notions by going through the tutorials: http://asp.net/mvc

Related

Implementing simple CRUD with OpenRasta and Web Forms

I've been asked to look into OpenRasta as an alternative to MVC ASP.NET at work, and as a starting point I'm trying to replicate the Movies tutorial from the MVC ASP.NET website.
I really like the ReST style of OpenRasta, and so far have got a simple database and a handler for GET based by ID, in the form of
ResourceSpace.Has.ResourcesOfType<Movie>()
.AtUri("/movie/{id}")
.HandledBy<MovieHandler>()
.RenderedByAspx("~/Views/MovieView.aspx");
I understand that use of POST and DELETE would allow me to create/update and delete items from my database, but unfortunately I'm stumped on how to do the views.
In the OpenRasta documentation it says:
When you use an aspx page as a view in OpenRasta, you essentially create a template to
generate content. As such, postbacks and events are not supported.
I might be being really dumb here, but would I be able to POST and DELETE from an ASP.NET page in the manner required by OpenRasta? I'm using a code-behind page, but that's not something I'm fixated upon.
I'm not that familiar with ASP.NET (haven't done any for ages), so I may be missing something obvious, but would really appreciate some pointers in the right direction.
What this means is that the postback model in asp.net webforms (aka the behavior by which the asp.net webforms infrastructure creates one massive form tag to post back asp.net specific data to a page continuously) is not supported, so any events you may be used to use on webforms controls will not work.
If you're used to MVC-styled interactions, you know how to use the form tag so you do as usual to create a new movie.
<form method="post">
<fieldset>
<input type="text" name="Name" />
<input type="submit" />
</fieldset>
The alternative is to do it in code using the webforms engine
<% using(scope(Xhtml.Form<Movie>().Post())) { %>
<%= Xhtml.TextBox<Movie>(_=>_.Name) %>
<% } >
And your handler code
public Movie Post(Movie movie) {
// create the movie instance in your db or whatever
return new OperationResult.SeeOther { RedirectLocation = movie.CreateUri() };
}
Code compiles in my head and may need a reality check before being put in a compiler.
Note that it's probably a good idea to move away from the webforms engine if you can, there are better alternatives (razor, spark, whatever you may decide to plug in).

Show a web form the way we do in WinForms

I am absolutely new to ASP.NET. In WinForms/ VB.NET we can display a form by
form2.show()
In my ASP.NET project, I have created a second webform but don't know how to show it.
How can this be done?
From your code-behind, you can redirect the user to the new page using the HttpResponse's Redirect method:
Response.Redirect("newPage.aspx")
If you want the user to self-navigate to the new page, use a hyperlink server control on your web form:
<asp:HyperLink id="hyperlink1"
NavigateUrl="~/newPage.aspx"
Text="My New Page"
runat="server"/>
You need to link to the second web form using a hyperlink, so in your first webform, having something like this:
My Second WebForm
With a web based programming model, you can't really apply the same programming model.
If you want something to appear as a popup, you could use javascript. I'd recommend taking a look a the jQuery UI Dialog.
Based on your question, I think your best option here is to first go and watch/read some tutorials on using ASP.NET. Programming for the web in a framework like ASP.NET is nothing like working with WinForms, even if you are using the same language (VB.NET). With ASP.NET WebForms they have tried to make the transition easier, but you still need to have a good grasp about how things work in the stateless web world if you really want to be effective. Then you can truly understand the other answers here and WHY you are doing that.

Add a usercontrol as the value of a html attribute

I am working in a CMS where we use tokens ( which is turned into a user control. Is there a way to add the user control into an attribute value for our template style?
example :
<div class="<$tokenName/$>" />
this currently outputs an encoded user control, which is then not parsed by IIS.
Short answer: this is not possible.
Longer answer...
It's not IIS's job to parse the control... that happens when IIS hands off the request to the ASP.NET engine. ASP.NET does a single-pass parse through your ASPX before the Page lifecycle even starts... this is why controls you delcare in the ASPX are available during the Init event. Whenever your CMS expands "$tokenName", you are far past the point at which ASP.NET is interpreting your markup.
If you're having trouble with that, here's a thought experiment for you: What happens when $token expands into a user control that has some other $token2 control embedded in it? And that control contains some other $token3? How many times are you going to try and parse/expand/interpret your markup?

ASP.NET MVC: Basic form question

A friend has asked me to help him with a project that's MVC. This is my first experience with MVC. I'm trying to build the MVC components for a form for him.
A page has a modal popup which uses a JavaScript to POST or GET and receive HTML backā€”that it displays inside the popup modal.
I need to create an MVC form that has validation to display inside aforementioned popup. This popped-up form will be used elsewhere on the site, so needs to be modular.
Should I create an ActionResult in my Route's Controller that returns an View (.aspx) containing my form? Should I instead create a Partial View (.ascx) that has the form, then use that Partial View in a View for my Route so other parts of the site--other Routes--can do the same?
I'm stuck at that basic understanding. From there, I don't even know what to do about the validation (was told the same validation will be used on a nearly identical form) and how a ViewModel fits into this like is used elsewhere on the site.
I've been reading a lot and looking at a lot of examples but I'm still confused.
You've got two separate questions here really. To deal with whether you should use a partial view or a regular view, just think of it this way. Does the HTML content in question represent a full page, or just a piece of a page that will be reused inside of other pages?
If it's the former, then use a full View. If it's the latter, then put it in a partial view. It sounds to me like you just need a full View. In either case, it's easy to change to the other if it turns out it's not meeting your needs.
As to validation, take a look at xVal if you're using ASP.Net MVC 1. It allows for easy validation using attributes on your models.
When you make your post from the modal popup do the validation then in the actionmethod you created specifically for that popup. If you want client side validation write up the js to do it.
As for the modal markup and what not just create a partial view for that, shouldn't be a big deal.

How can I emulate the ASP.NET Ajax UpdatePanel when using ASP.NET MVC?

In "traditional" ASP.NET (Web Forms), the UpdatePanel control lets you do a partial refresh of a part of a page. You don't need to write much code to hook this up.
What's the equivalent in ASP.NET MVC? I'm guessing I'd need to use a partial view for the bit that I want to update, then on the client side retrieve that HTML and pump it into the innerHtml of the correct DIV?
A tutorial/example would be helpful...
A quick google with your tags throws up a bunch. This is pretty good:
http://www.iridescence.no/post/Partial-Rendering-with-ASPNET-MVC-and-jQuery.aspx
And yes, you're just calling a controller to return rendered HTML which you then inject. Much lighter than the updatepanel and almost as easy to implement.
Have a look at Superload. I think it's the closest thing to the update panel that I've seen for asp.net mvc. Otherwise you can use jquery ajax, .load() or .get(), to update any parts of the page or divs. You just have to manually set the parameters as needed.

Resources