MVC3 - razor : How to use mailto dynamically - html

I have a link as bellow. Is there any way to change info#example.co.uk with model.Contact.EmailAddress to make it dynamics.
#Html.LabelFor(model => model.Contact.EmailAddress)</dd>

If the Contact is a property of your model which is binded to your view, you can use it like this
#Model.Contact.EmailAddress
If it is a child property of your model and you want to do it inside a loop, you can do like this
#foreach (var item in Model.Contacts)
{
#item.Contact.EmailAddress
}

Simply use the appropriate razor syntax inline with your html as you would normally do when writing any output from your model.
#Html.LabelFor(model => model.Contact.EmailAddress)

Related

How to properly concatenate multiple variables in asp.net core ViewData[] object of a view

Up at top of my view:
#{
ViewData["Title"] = #Html.DisplayFor(model => model.SETitle);
ViewData["Description"] = #Html.DisplayFor(model => model.SEDescription);
ViewData["canonical"] = String.Concat("https://example.com/", Html.DisplayFor(model => model.CategoryURL), "/", Html.DisplayFor(model => model.URLSlug));
}
This renders in the browser like so:
<link rel="canonical" href="https://example.com/Microsoft.AspNetCore.Mvc.ViewFeatures.StringHtmlContent/Microsoft.AspNetCore.Mvc.ViewFeatures.StringHtmlContent">
I am using a standard asp.net core template. And it seems like this will always occur if you are concatenating multiple variables together for a ViewData[] string. What do I need to change about my syntax to get this actual values to pass through and render in my browser?
All your properties appear to be type of string so there is no need to use Html.DisplayFor() (but if you do, then it needs to be #Html.DisplayFor() - note the leading # so the result of the method is output).
Instead you can just use #Model.CategoryURL and #Model.URLSlug.
And if https://example.com/ is your site, then you should be doing this using the Url.Action() method to generate the correct relative url without hard-coding the site name.
In addition, you have commented that these values are being used in your layout, in which case, consider using base view model containing those properties and set them in the controller method so that the Layout uses #model yourBaseViewModel, and each view that uses that layout inherits from your base view model

Umbraco Razor query - how to implement "does not contain"?

In Razor how would I write a query where property does not contain or excludes?
Basically looking for the opposite of:
Where(x => x.GetProperty<String>("myCategory").Contains(myString))
Uhm. Unless I'm missing something:
Where(x => !x.GetProperty<String>("myCategory").Contains(myString))
:-)

Possible to add HTML input Value attribute in Razor for MVC 5?

I'm trying to find a solution to have a value inside the input form at start, but I can't find any useful info and therefore I wonder if it's possible att all? When I right click one the webpage, I can see that there are a empty value, so perhaps it's is possible?
One solution would be to use plan HTML and not the #Html.EditorFor But I guess it's better to use Razor.
#Html.EditorFor(model => model.Test, new { htmlAttributes = new { #class = "form-control" } })

Viewmodel in partial and duplicates

I have really strange problem. So, my structure is like this
<div id="tasks">
#Html.Partial("_Tasks", tasks)
</div>
and in _Tasks I do a foreach through all tasks and for each task I have additional partial
...
#Html.Partial("_Time", new TimeViewModel(task))
...
and inside of _Time I have form
...
#Html.TextBoxFor(m => m.Name)
....
So in a view I render a partial and then inside again multiple partials and inside it a form. When I perform a page load, it works. Problem begins with when I'm using ajax, so I perform edit to Time and post to server and update #tasks with returned html.
I'm my controller action I have...
...
return View("_Tasks", tasks);
and the problem now is that all inputs generated by #Html.TextBoxFor(m => m.Name) have the same value. Why? If I do
#Html.DisplayFor(m => m.Name)
I works just fine. I also tried with
#Html.TextBoxFor(m => m.Name, new { Value = Model.Name })
and it works, but it looks hackish to me.
The question is, why is get this behavior? Why does all TextBoxFor have same value?
Default model binder in ASP.NET MVC determines how to map the values to your model by using name attributes on input tags. All the html helpers generate markup with that in mind.
So when you write
#Html.TextBoxFor(m => m.Name)
It generates something like this:
<input type="text" name="Name" />
However when you use this helper in a partial view and try to bind a model that's a property of the main model, it won't work. Html helper in the partial will still generate markup thinking its model is the main model. So your input will be binded to main model's Name property. You can take a look a this question for solving this problem. Also keep in mind the default binder can not pick up complex collections when you handle post requests.

add additional class to render partial

I'm looking for some help regarding partials. I have created a partial for a menu on my site, but I would like to know whether I can add an additional class to a partial. More specifically, I am looking to assign an additional class to an li menu object so that I can indicate that the user is on that particular page.
Here is my code for the partial (I'm using HAML). It's named _menu.html.haml and it is located in the 'shared' folder.
.three.columns
%ul#nav
%li
%a{:href => "../pages/about"} About
%li
%a{:href => "../pages/careers"} Careers
%li
%a{:href => "../pages/contact"} Contact Us
%li
My code for the carrers.html.haml page is as follows:
code...
= render :partial => 'shared/menu'
more code...
I would like to add a .active_page class to the careers link located in the partial. This class changes the background image behind the text to indicate that the user is on a specific page. More specifically, I'd like it to look like so:
%li.active_page
%a{:href => "../pages/careers"} Careers
Is it possible to do this using partials?
The fact that you're using a partial doesn't change how you're able to use HAML, or ERB, or anything like that. If you want to add a class, yes, then just add .active_page on to an HTML tag. If it's a valid thing to do in HAML, then you can do it anywhere in a view, even if it happens to be a partial.
A partial is just modularized/reusable views. Just like you can use code inside an instance method and inside a class method, because they're just code, so can you use the same HAML code in a partial and in a "regular" view.
There are a bunch of ways to do this. I would not use partials, I would do:
%li{:class => #active_page=="careers" ? "active_page" : ""}
%a{:href => "../pages/careers"} Careers
Assuming #active_page has been created by your controller.