Friday, February 11, 2011

ASP.NET MVC 2 strongly typed htmlhelper, indexes

public class Foo { public bool Checked {get;set;}}

View:

<viewdata model="Foo[] fooList" />
<for each="var f in fooList">
    ${Html.CheckBoxFor(x=>x[fIndex].Checked)}
</for>

Will output:

<input id="Checked" name="Checked" type="checkbox" value="true" />
<input name="Checked" type="hidden" value="false" />

<input id="Checked" name="Checked" type="checkbox" value="true" />
<input name="Checked" type="hidden" value="false" />

<input id="Checked" name="Checked" type="checkbox" value="true" />
<input name="Checked" type="hidden" value="false" />

Problem is that System.Web.Mvc.ExpressionHelper.GetExpressionText does not include index in id/name.

That leads to problems in case I want to add a label for every checkbox (because all id`s are the same).

Any ideas how to handle this properly?


From the MVC source=>

 while (part != null) {
            if (part.NodeType == System.Linq.Expressions.ExpressionType.MemberAccess) {
                MemberExpression memberExpressionPart = (MemberExpression)part;
                nameParts.Push(memberExpressionPart.Member.Name);
                part = memberExpressionPart.Expression;
            }
            else {
                //arghhhh... [index] != MemberAccess :(
                break;
            }
        }
  • Use the CheckBoxFor overload that allows you to specify html attributes:

    CheckBoxFor(TModel)(HtmlHelper(TModel), Expression(Func(TModel, Boolean)), IDictionary(String, Object))
    

    For example,

    ${Html.CheckBoxFor(x => x[fIndex].Checked, new { id = "foo" + fIndex) })}
    
    Arnis L. : That's not **properly**. In this case - let me ask what's the benefit of `CheckBoxFor` 'strongly-typed' helper method if i won't be able to get that id back from view model without messing around with hard-coded strings? That's the same as to use `${Html.CheckBox("foo"+fIndex,m.AllCatalogs[i].Checked)}` which even looks better.
    Brettski : @Arnis L. Agreed
    From Ajw
  • The ability of the expression-based helpers to understand indexes isn't in the product yet. It will be in the next preview release (whatever comes after MVC 2 RC). See http://aspnet.codeplex.com/WorkItem/View.aspx?WorkItemId=4970.

    Arnis L. : Your are hero of the day. :P
    From Levi

Differences in WCF 2.0 framework vs WCF 3.5 framework

Hi, I am wondering if there are any updates / differences to WCF from 2.0 framework to the 3.5 framework?

Thanks

automatically close batch file when done

How would I make this close itself when its done?

copy h2.cfg default.cfg /y
c:
cd "c:\program\reba"
"c:\program\reba\reba.exe"

i tried adding:

cls
@exit

in the end but i didnt work

edit: i want the cmd window to close when reba has loaded

  • You'll need to run reba.exe in the background.

    The shell command START should do the trick for you. Here is some documentation on it:

    http://ss64.com/nt/start.html

    I think you can say something like

    START "" "c:\program\reba\reba.exe"
    

    in your batch file (i.e. just add the START).

    Joey : Note that you should never blindly use spaces around the first argument when dealing with `start`. This causes the argument to be interpreted as the window title for the new window and then the program to run is missing. Which means it will just spawn a new `cmd` window with the respective window title.
    Carl Smotricz : Ah, very interesting! That's why the doc I quoted makes those mysterious admonitions that you should always include a title, without giving a sensible reason why. This clears things up, thanks!

How i Can Decrease the loading time of Crystal Report File

Hi

How i can Decrease the report file Loading Time? I Use this Code to Load a Report File

reportDocumnet.Load(reortFilePath);

but the first time loading is very long.

just in first time running this code

i want use a way to run this line of my code speedly

  • Some people load a report in background thread on program startup. Then the massive crystal libraries will be loaded when user runs the first report.

    hosseinsinohe : that right i want use other ways pleaze help me
    From dotjoe
  • How much time spent in the SQL/data plan generation? Can you create a view in your database to speed up the content resolution? Can you cache previously generated reports that have the same query parameters?

    hosseinsinohe : just this line of my code runing in long time(in first time) And this not realated to load data
  • To follow on with what has been said about preloading crystal libraries have a look at this article

    http://www.google.co.uk/search?hl=en&source=hp&q=http%3A%2F%2Fwww.experts-exchange.com%2FDatabase%2FReporting_%2FCrystal_Reports%2FQ_24295843.html&btnG=Google+Search&meta=&aq=f&oq=

    dotjoe : bold move linking to expert sexchange :) why can you only see the answers if linked from google search? http://www.google.com/search?q=Slow+opening+of+Crystal+Reports+in+.NET+Run+time&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a
    David : @dotjoe Bizarre! Changed link to ensure anyone using will reach answer. Thanks.
    From David

springs authentication, does it use encrypted cookies?

Does the spring framework use (or in one of the options that it supports) encrypted cookies that store the logged in users userId in a cookie?

This is how asp.net authentication works, where it encrypts a value in a cookie, which is normally the userId or username.

Is that what spring does? (I realize spring let's you choose, but is this the most common approach generally?)

  • Storing user ID or any kind of data the server relies upon is a terrible idea. It typically means as soon as someone figures out how your encryption works (which is only a matter of time, particularly when they have a crib as user IDs tend to be public too) they can probably compromise your system.

    In case you're wondering what a "crib" is. see Cryptography FAQ (03/10: Basic Cryptology):

    Cryptanalytic methods include what is known as practical cryptanalysis'': the enemy doesn't have to just stare at your ciphertext until he figures out the plaintext. For instance, he might assumecribs''---stretches of probable plaintext. If the crib is correct then he might be able to deduce the key and then decipher the rest of the message. Or he might exploit ``isologs''---the same plaintext enciphered in several cryptosystems or several keys. Thus he might obtain solutions even when cryptanalytic theory says he doesn't have a chance.

    Java Web apps typically just store a session ID and that session on the serverside contains such information as user ID. That's much more secure.

    mrblah : Funny how MS doesn't think so :) Probably 95% of .net apps store either the user id or username in the cookie!
    matt b : do you have a source for this claim?
    cletus : If you have a system where the user just needs to figure out a) what encryption algorithm you use (usually pretty obvious) and b) what the encryption key is (for which you may have one or many cribs) in order to log in as anybody then you don't need to prove that system isn't insecure. It's basic commonsense.
    mrblah : @matt look here: http://msdn.microsoft.com/en-us/library/aa480476.aspx see: FormsAuthentication.SetAuthCookie(userName.Text, false);
    From cletus
  • I don't have the source handy to prove this, but the answer to the question is no.

    Spring Security handles everything on the server side. The only cookie on the client is the one for JSESSIONID, and the security framework merely checks for the authentication/principal object in the request's session (at least under the default setup).

    I don't understand why you would store any sort of authentication information in the client's cookie if you could simply store a sessionID and track authentication details and state on the server side.

    mrblah : sessions are bound to a specific server.
    matt b : and a load balancer with sticky sessions makes that a moot point
    mrblah : true, but who wants sticky sessions if you have other options.
    matt b : people that don't want to put user information in cookies or worry about encryption :) or apps that need to maintain state during a user's session.
    From matt b

Simple XML HELP !!!!

Hi ,

I need some help ,

All I need to do is change "HERE" to a the value of $date

$line1 = $sxe->addChild("date","HERE");

How can I add the value of $date into the area were "HERE" is ?

Please help

  • $sxe->addChild("date", $date);
    
    Oliver Bayes-Shelton : just changed it too $sxe->addChild('date', $date); and it works :) Thanks
    From Ben Marini
  • EDIT: Actually the following wont work unless the date node exists. Listen to the others... We see how often i actually modify structure with SimpleXml ;-)

    $sxe->date = $date;

How to use php value in SimpleXMLElement ?

Hi ,

I know that too add a text value into the xml page I do the following

$person = $sxe->addChild("person");
$person->addChild('first_name');

If I want to use the value of $person how could I code it ?

  • You can get it as

    $personXml = $person->asXML();
    
    From alex