0

hi guys i am new to mvc and i am working on one project...where in my view i need to get a substring of a string in @html.DisplayFor(modelItem=> item.resumelink).

i tried couple of things like

    <span>
        @Html.LabelFor(modelItem => item.ResumeLink,new {text=item.ResumeLink.Contains("#") ? item.ResumeLink.Substring(0, item.ResumeLink.IndexOf("#")) : item.ResumeLink })
    </span>

but it is not working.

i am trying to create display template but i dont know how to get the last index of a file till where i want to crop my string...any help would be appreciated..thnks

SamGhatak
  • 1,487
  • 1
  • 16
  • 27
  • PLease show the string and the substring which you want to get from string – Mairaj Ahmad Mar 11 '16 at 09:58
  • Do u want to display the substring to the user ? – REDEVI_ Mar 11 '16 at 10:16
  • ok i got rid of the thing i wanted but still something is missing...i declared one more porperty in a class that will get a substring of a prev property public string ResumeSubString { get { return ResumeLink.Contains("#") ? ResumeLink.Substring(0, ResumeLink.IndexOf("#")) : ResumeLink; } } but that removes extension of a file also...but i need to display extention of a file just by removing guid after filename – Karan Trivedi Mar 11 '16 at 10:21
  • like i have file name about us#fb40c127-9501-40ad-807c-9b8216348005.docx but i want to show about us.docx...by only removing #fb40c127-9501-40ad-807c-9b8216348005 from a string – Karan Trivedi Mar 11 '16 at 10:24

3 Answers3

0
@Html.ActionLink(modelItem => item.ResumeLink, modelItem => item.ResumeLink, new {text=modelItem => item.ResumeLink.Substring(0,2) }) 

This will generate a url like this

Controller/action/text=20    // i cut the value of 2014
REDEVI_
  • 684
  • 8
  • 18
  • that will just display ResumeLink to the user for a LableFor methor...so i am using DisplayFor insted of LabelFor....and i created a property which will give substring of a another propery. – Karan Trivedi Mar 11 '16 at 10:29
  • do u want the text value to be available in another action method ?? – REDEVI_ Mar 11 '16 at 10:32
  • the displayfor or the labelfor overloads stores the values as html attributes , if you want the user to click on the link and get the value there by redirecting him to different action method use should use the @html.ActionLink helper – REDEVI_ Mar 11 '16 at 10:35
  • yes but i did try @Html.LabelFor(modelItem => item.ResumeLink,new {text=item.ResumeLink.Contains("#") ? item.ResumeLink.Substring(0, item.ResumeLink.IndexOf("#")) : item.ResumeLink }) but it doesnt work...so what i did is creaTED A ANOTHER PROPERTY IN A MODEL CLASS public string ResumeLink { get; set; } public string ResumeSubString { get { return ResumeLink.Contains("#") ? ResumeLink.Substring(0, ResumeLink.IndexOf("#")) : ResumeLink; } and in a view file@Html.DisplayFor(modelItem =>item.ResumeSubString) – Karan Trivedi Mar 11 '16 at 10:40
  • i think this should work @Html.ActionLink(modelItem => item.ResumeLink, modelItem => item.ResumeLink, new {text=modelItem => item.ResumeLink.Substring(0,2) }) – REDEVI_ Mar 11 '16 at 10:46
  • can u just tell if i have a string : about us#fb40c127-9501-40ad-807c-9b8216348005.docx and i just need a substring as: about us.docx by removing: #fb40c127-9501-40ad-807c-9b8216348005 from the origional string so how to get it...m confused with indexOF parameters as i am a noob in this – Karan Trivedi Mar 11 '16 at 10:50
  • you should use text.TrimStart('#') if you are sure about what is the ending characters , else you have to use Regex – REDEVI_ Mar 11 '16 at 10:55
  • refer http://stackoverflow.com/questions/1359412/c-sharp-remove-text-in-between-delimiters-in-a-string-regex – REDEVI_ Mar 11 '16 at 10:55
0

Just keep the code clean this way, the idea used here is.. find the index of # then find the index of . and remove all the characters in this index range.

<span>
     @{
        var filename = item.ResumeLink; 
        var actualFileName = filename.Contains("#") ? file.Remove(file.IndexOf('#'), (file.IndexOf('.') - file.IndexOf('#'))) : filename ; 
      }
     @Html.LabelFor(modelItem => item.ResumeLink,new {text= @actualFileName })
  </span>

Result if item.ResumeLink = us#fb40c127-9501-40ad-807c-9b8216348005.docx

"us.docx"
Rajshekar Reddy
  • 18,647
  • 3
  • 40
  • 59
0

in a view i write ` @Html.DisplayFor(modelItem => item.ResumeSubString)

       @*<a href="#" /> <span>@Html.LabelFor(modelItem => item.ResumeLink,new {text=item.ResumeLink.Contains("#") ? item.ResumeLink.Substring(0, item.ResumeLink.IndexOf("#")) : item.ResumeLink })</span>*@
    </td>`

and in a class there is something like following

public string ResumeLink { get; set; }
    public string ResumeSubString
    {

        get
        {
            var filename = ResumeLink;
            var actualFileName = filename.Contains("#") ? filename.Remove(filename.IndexOf('#'), (filename.IndexOf('.') - filename.IndexOf('#'))) : filename;
            return actualFileName;
        }
    }

thnx reddy and guys whoever bother to reply :)