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

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

Related

How to filter null values with Kartik grid and select2 filter?

I'm trying to add a custom option to my select2 filter (kartik grid). This option is meant to select all rows where something_id=null. It's a simple crud generated grid, modified to use kartik grid and select2 filter instead of yii2 default grid/filter.
I tried doing this in my table2_id column:
'filter' => [null => 'Nothing'] + Arrayhelper::map(Something::find()->orderBy('id)->asArray()->all(), 'id', 'thing')
But this new "null" option is not showing in the menu. I assume for select2 null just mean no filter and select2 just shows the placeholder for that. I can't find any option in select2 documentation to modify this behavior. The generated html for this option is:
<option value="" selected="" data-select2-id="5">Nothing</option>
But the css has the property visibility:hidden;. So I tried using:
'filter' => [0 => 'Nothing'] + Arrayhelper::map(Something::find()->orderBy('id)->asArray()->all(), 'id', 'thing')
But I can't manage to properly translate 'something_id'=>0 into a 'something_id'=>null condition neither in the controller or search model. Tried many things but everything failed at some point and I don't think my code deserves to be posted here, also I don't keep a copy of every failed piece of code so I'd like to ask what is the correct way of doing this from scratch. Thank you.
Well, my answer got deleted because I made a joke. Here is the very very serious version of my answer.
// WatheverSearch.php
if ( $this->something_id === '0' ) {
$query->andWhere(['something_id' => null]);
}else{
$query->andFilterWhere(['something' => $this->something]);
}

(m => m.Email) meaning (ASP.NET) and how to get a user's username

I am brand new to web development. What little experience I have is in C# Unity and Java. The code below was taken from the screen followed by the Google authentication/login screen. I am trying to set the user's Google name to the username on the website. I was wondering if someone could help me out and maybe explain what (m => m.Email) means.
Thank you so much!
#model Start.Models.ExternalLoginConfirmationViewModel
#using (Html.BeginForm("ExternalLoginConfirmation", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { #class = "form-horizontal", role = "form" })) {
#Html.AntiForgeryToken()
<hr />
<div class="form-group">
<div class="col-md-10">
#Html.TextBoxFor(m => m.Email)
</div>
</div>
<input type="submit" />
}
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}
in the code #Html.TextBoxFor(m => m.Email) the variable m represents the current model, which is set at the top of the file as Start.Models.ExternalLoginConfirmationViewModel.
m => m.Email means "get the Email property from this model". If you were to look at the model (in Visual Studio put your mouse cursor on ExternalLoginConfirmationViewModel and press F12) you will most likely see that there is an Email property on it.
So #Html.TextBoxFor(m => m.Email) creates the HTML needed to show a textbox for the email address of the current model.
In the end it will probably generate some HTML that looks like this:
<input type="text" value="bob#example.com" id="some-generated-id-here" />
This is using the MVC Razor syntax, which you might want to do some reading up on.
https://learn.microsoft.com/en-us/aspnet/web-pages/overview/getting-started/introducing-razor-syntax-c
https://learn.microsoft.com/en-us/aspnet/core/mvc/views/razor
http://www.tutorialsteacher.com/mvc/razor-syntax
Here's the slightly more technical explanation. The => operator denotes a lambda, which can be thought of as a function that returns its value in place. The stuff on the left side (m) is the parameter(s) to the "function", and the stuff on the right side is what will be returned from the "function". Given that, m => m.Email means, essentially, "return the Email property of what I pass in here".
However, when it comes to things like HtmlHelpers, what you're passing as the first parameter there is actually Expression<Func<TModel, TProperty>>, so let's unwrap that. Func<TModel, TProperty> is the actual type of that lambda we just talked about. Func is a generic type, with two (or more) type parameters. Here, the first type parameter is TModel, and corresponds to the type of the "left side" of the lamda, i.e. the type of the thing we're passing in. The TProperty type parameter is the type of the "right side" of the lambda, i.e. the thing we're returning.
Expression is a wrapper applied to things like Func, and builds what's called an "expression tree" out of the lambda. This expression tree, among other things, allows the inspection of what's inside that lambda, so you can determine things like the name of the property that's being returned. The HtmlHelpers, then, use this information to do things like generating id and name attributes, which obviously need to match the property name. This is actually an important thing to recognize as a lot of new developers get confused and think they can pass in just about anything to an HtmlHelper as the expression. That's not the case, because you're not dealing with a value, but rather an expression representing a value. For example, the following would not work at all:
#Html.TextBoxFor(m => m.Email.ToLower())
Because it's not a valid expression, even though it works for the purpose of the lambda.

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.

MVC3 how to get innerHtml from the return sting of #Html.ValidationMessageFor<> method?

I want to show the validation message summary in the tool tip of an html box. Something like this,
#Html.TextBoxFor(m => m.Contract.PrimaryContact.FirstName, new { #class = "quarter", title = #Html.ValidationMessageFor(m => m.Contract.PrimaryContact.FirstName) })
How can I do it? As of now am seeing the entire html mark up of the validation message in the title. Is there a way I can just see the validation message?
Thanks!
I'm afraid the only proper way is to reimplement the Validation helper methods. Fortunately it isn't hard and the source is available online.

MVC3 - razor : How to use mailto dynamically

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)