How to use datalist in mvc3 - asp.net-mvc

I am new in mvc. i am working with mvc project .it's a shopping cart project. In asp.net web forms we used data list control and we also set a RepeatColumns property like 5,10,20. What is alternate to that in MVC? Can I use the same control in MVC as well?
so can any one please tell me how to show my data in data list control?

To have five product Image in one row you can use two loops and do this in razor syntax....
<div>
<table>
#for(int i = 0; i <= (Model.Count - 1) / 5; ++i) /*Five is no of product record count per row*/
{
<tr>
for(int j = 0; j < 5 && i + j < Model.Count; ++j) {
<td style="width:240px;margin-left:30px; margin-right:30px;"> /*Adjust based on your HTML and CSS requeirment*/
<img src="#Product.ImageSource" alt="drawing" />
<Div>#Product.Title</Div>
</td>
}
</tr>
}
</table>
</div>
Above can be used to produce 5 Product Images per row....

well theoretically there are no server controls available in asp.net MVC.. For learning you can check what are the other differences in asp.net web form and MVC peradigm and must read this link. So you get overview what you can do and can't in MVC.
Now to display repeatable data in MVC you need to use LOOP and presant usign HTML helpers. You can adopt RAZOR syntax or Asp.net Syntax to show the data.
E.g in Razor you can have something like following to mimic the Repeater requirements.
<div class="container">
<header>
<h3>Auctions</h3>
</header>
<ul id="auctions">
#foreach(var auction in Model.Auctions) {
<li>
<h4 class="title">
#auction.Title
</h4>
</li>
}
</ul>
</div>
TO quickly learn what is MVC and how to do Bookbinding you can refer Music Store tutorial by Microsoft that explains basic necessarily things for MVC learner.
You question is little off topic but I love to answer them. ;) Happy coding

Related

Inject comment with partial name during RenderPartial - asp.net razor

mvc4, razor viewengine. It would be useful if, when in debug mode, a snippet like this that includes a partial would leave a comment saying what partial it injected. Are there extensibility points that I just can't find? Obviously I could just type out this comment in the partial views, but that sounds like WAAYY too much work and isn't fun for anybody.
index.cshtml
<div class="somecontainer">
#foreach(var thing in Model.Things) {
Html.Partial("ThingPartial",thing)
}
</div>
ThingPartial.cshtml
#model ThingModel
<h1>#Model.Name<h1>
<p>#Model.Description</p>
would generate html like this
<!--razorView:things/index-->
<div class="somecontainer">
<!--partialView:ThingPartial-->
<h1>Name 1<h1>
<p>Description 1</p>
<!--partialView:ThingPartial-->
<h1>Name 2<h1>
<p>Description 2</p>
<!--partialView:ThingPartial-->
<h1>Name 3<h1>
<p>Description 3</p>
</div>
Motivation
I want my testers to be able to use the comments to write more accurate bug reports without my developers / me having to do anything extra!
What I have in my mind is something configurable in global.asax.cs, like so
ViewEngines.Engines.Clear();
RazorViewEngine viewEngineToAdd = new RazorViewEngine();
//I COMPLETELY MADE THIS LINE UP, THESE THINGS DON'T EXIST (but I want them to!
viewEngineToAdd.PreRender += (OutputStream,IView) => OutputStream.Write(#"<!--"+IView.Name+"-->")
ViewEngines.Engines.Add(viewEngineToAdd);
Try this:
<div class="somecontainer">
#foreach(var thing in Model.Things) {
Html.Raw("<!--" + thing + "-->");
Html.Partial("ThingPartial",thing)
}
</div>

Can ASP.net pages be used in ASP.net MVC?

"You can use existing ASP.NET features with the ASP.NET MVC framework, such as nested master pages, in-line expressions (<%= %>), declarative server controls, templates, data-binding, localization, and so on." - MSDN
Does that mean we can use the server controls in ASP.net web forms in MVC?
Thanks,
Any controls that post back to the server don't work with MVC because MVC doesn't use the same mechanism or page cycle to post back to the same page. Rather MVC pages are routed to a new URL, so there's no concept of a 'persistant' page as there is with Web Forms.
There's a lot of discussion about how this sort of thing should be handled in an MVC framework because there's no doubt that Web Forms make many things very easy. Currently in MVC the closest thing to 'controls' are the Mvc HTML Helper classes that provide simple (but not very configurable) control rendering.
Using MVC vs. Web Forms is full of trade offs (in both directions) and there's no doubt you'll end up writing more markup code in MVC to make up for what you lose with Web Forms. The advantage is that you have much mroe control over rendering and aren't tied to the sometimes complex page pipeline cycle of postbacks.
For Syntax,
Razor:
<ul>
#for (int i = 0; i < 10; i++) {
<li>#i</li>
}
</ul>
Web Form in mvc(You can use this too)
<ul>
<% for (int i = 0; i < 10; i++) { %>
<li><% =i %></li>
<% } %>
</ul>
İf you want to check more information please check below link
hope helps you.
Using ASP.NET Server Controls in MVC?

How do you implement the "add comment" feature used in Stackoverflow for asp.net mvc?

How do I recreate the add comment feature in Stackoverflow using mvc 3 razor (EF4)?
Here's mockup code:
<div>add comment</div>
<ul id="comments">
#foreach (var comment in Model) {
<li>#comment</li>
}
</ul>
<form method="post" id="commentForm"
action="#Url.Action("AddComment")">
#Html.TextArea("Comment", new { rows = 5, cols = 50 })
<br />
<input type="submit" value="Add Comment" />
</form>
How do I go about with the Add/Edit comments via ajax/jquery? Should it be a partial view?
I'm not sure how StackOverflow does it, but there are a few ways to achieve the same thing.
Usually, you'd write some jQuery in your page to intercept the form post and post the data using ajax instead.
You could post to a Web API or just an action that returns JSON. On the server, add the comment to whatever data store you're using and reply with either a success result or details of the saved comment (up to you). When complete, render the new entry on the client side using jQuery - in your case, just add a new li with the comment. OR
You could post to an action that returns a partial view. Save the comment, update the collection, then render the partial view. In your jQuery, you could replace the whole comments section with the new content.
It's a very broad question and there are many other ways to do it. You could use something like SignalR to push new comments from the server (to make it realtime), and you could use a JS templating framework like Knockout JS so you're only dealing with a template and an array of objects.

mvc+jqm actionlink html text issue

I'm just starting out with MVC, and I'm trying to build a sample, proof of concept page for mvc interaction with jquery-mobile. I've searched for an answer on this, but I don't think I know enough yet to formulate proper queries.
On my view, I am trying to generate output that functions like this (sampled from the jqm demo site) :
<ul data-role="listview" data-inset="true">
<li><a href="index.html">
<h3>Stephen Weber</h3>
<p><strong>You've been invited to a meeting at Filament Group in Boston, MA</strong></p>
<p>Hey Stephen, if you're available at 10am tomorrow, we've got a meeting with the jQuery team.</p>
<p class="ui-li-aside"><strong>6:24</strong>PM</p>
</a></li>
<li><a href="index.html">
<h3>jQuery Team</h3>
<p><strong>Boston Conference Planning</strong></p>
<p>In preparation for the upcoming conference in Boston, we need to start gathering a list of sponsors and speakers.</p>
<p class="ui-li-aside"><strong>9:18</strong>AM</p>
</a></li>
</ul>
So, I'm working it like this :
#foreach (var item in Model) {
<li>
#Html.ActionLink( LargeExpandedMultiContentText, "Edit", new { id = item.SysID })
</li>
}
My question, which I don't know a simple way of asking, is, how, in MVC terms, do I fill the variable, so that the href generated by ActionLink() matches the style I've sampled from the jqm website?
I've tried this, and while it almost works, I'm sure its not the 'right' way, especially when I get to a spot of pulling "Steven Weber" and other strings out the the item/Model...plus it encodes the text, but I'm sure there's a way to deal with that easy enough.
#foreach (var item in Model) {
<li>
#{ String txt = "<h3>Stephen Weber</h3><p><strong>You've been invited to a meeting at Filament Group in Boston, MA</strong></p><p>Hey Stephen, if you're available at 10am tomorrow, we've got a meeting with the jQuery team.</p> <p class=\"ui-li-aside\"><strong>6:24</strong>PM</p>";
}
#Html.ActionLink( txt, "Edit", new { id = item.SysID })
</li>
}
Thanks, I appreciate your help
Brian
I'm not sure it's possible to use Html.ActionLink to build such a complex action link. That said, the Html Helpers are just supposed to be helper functions to make your life easier by giving you shortcuts to render HTML in ways commonly required. You don't need to use them. In this case, I think instead you should use a normal html tag, and Url.Action to create the correct URL.
If you wanted to include this information within an enumerable model I'd probably do something like:
#foreach (var item in Model) {
<li><a href="#Url.Action(item.Action)">
<h3>#item.Name</h3>
<p><strong>#item.Title</strong></p>
<p>#item.Message</p>
<p class="ui-li-aside"><strong>#item.Time</strong>#item.TimePeriod</p>
</a></li>
}
Note: the function you were looking for to display contents without encoding them is #Html.Raw
In answer to my other 'half question' I found this resource...
http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx
I believe the "razor delegate" item there at the bottom would fit the need I had.
#{
Func<dynamic, object> b =
#<strong>#item</strong>;
}
#b("Bold this")

asp.net mvc razor vb.net for loop syntax

I am new to MVC and razor syntax .Trying to create c#.net code for viewbag but i am not able to find any value of viewbag like the example a viewbag.Message and viewbag.Numtimes
<ul>
#for (int i=0; i < ViewBag.NumTimes; i++) {
<li>#ViewBag.Message</li>
}
</ul>
It's not very different than C#, really. Just using the VB.NET syntax...
<ul>
#For i = 0 To ViewBag.NumTimes-1
#<li>#ViewBag.Message</li>
Next i
</ul>
A good reference with many examples is also here:
http://www.asp.net/web-pages/tutorials/basics/asp-net-web-pages-visual-basic
Edit: Expanded answer a bit and added a link to a nice introduction to VB.NET+Razor
<ul>
#For i=0 To (ViewBag.NumTimes-1)
<li>#ViewBag.Message</li>
Next i
</ul>
For more information see here:
http://www.w3schools.com/aspnet/razor_vb_loops.asp

Resources