hi i want the substring of a string in @html.displayfor(modelItem=>item.resumelink)

756 Views Asked by At

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

3

There are 3 best solutions below

9
On BEST ANSWER

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"
8
On
@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
0
On

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 :)