How do I get rid of cannot convert method group "TextBoxFor" to non-delegate type "object"? - html

first time using asp.net and MVC4 on visual studio trying to create a simple form for customer to enter details for a flight booking assignment, and have this error (shown below).
I cant seem to fix it, have no idea where to look to fix it to be honest. Is there another way of creating an easier form or can someone help fix this ?
View:
#model WebsiteApplicationASSES.Models.customerdetails
#{
ViewBag.Title = "customerBooking";
}
<!DOCTYPE html />
<html></html>
</div>
<div>
#using (Html.BeginForm())
{
<table>
<tr>
<td> #Html.Label ("First Name:")</td>
<td> #Html.TextBoxFor (x=>x.customerFirstName)</td>
</tr>
}
Model:
namespace WebsiteApplicationASSES.Models
{
public class customerdetails
{
public string customerFirstName { set; get; }
public string customerSecondName { set; get; }
public int customerAge { set; get; }
public int customerTelephoneNumber { set; get; }
public string customerEmail { set; get; }
public int numberOfPassangers { set; get; }
public int passangersAge { set; get; }
}
}
""
thank you!

Try removing the space. The Razor parser will try to invoke #Html.TextBoxFor as method group and treat (x=>x.customerFirstName) as text.
It should be:
#Html.TextBoxFor(x=>x.customerFirstName)
In normal C# this would work. However, with Razor one-liners can't contain spaces, unless you surround them with parentheses: #(Html.TextBoxFor (x=>x.customerFirstName))

Related

How do you link to other razor pages and include a query string parameter in Blazor WASM?

I've got a Blazor WASM app. It has two razor pages:
Documentation.razor:
#page "/documentation"
ViewRecord.razor:
#page "/documentation/ViewRecord"
I have a DocumentationController too.
I want to create a few hyperlinks within the Documentation razor page that have hyperlinks in this format:
/Documentation/ViewRecord?recordtype=randomWord1
/Documentation/ViewRecord?recordtype=randomWord2
/Documentation/ViewRecord?recordtype=randomWord3
Is there a cleaner way to do this, similar to using ActionLinks, instead of having to do something like this:
link1
link2
link3
This is the beauty of Blazor and Razor Components, if you find you want to something, you can create a re-usable component for it yourself. Complete flexibility. Say for instance we have the following component:
UrlBuilder.razor
#if (!string.IsNullOrEmpty(FullUrl))
{
#LinkDesc
}
#code
{
[Parameter]
public string LinkDesc { get; set; }
[Parameter]
public string Controller { get; set; }
[Parameter]
public string Action { get; set; }
[Parameter]
public string UrlParameter { get; set; }
private string FullUrl { get; set; }
protected override void OnInitialized()
{
FullUrl = $"{Controller}/{Action}?{UrlParameter}";
}
}
You can then access that component anywhere through your application like so:
<UrlBuilder LinkDesc="link 1" Controller="Documentation" Action="ViewRecord" UrlParameter="#word3" />
Is that any easier than creating a a href manually? Absolutely not, however, you could customize it to your delight.

Lambda Expression - Error Cannot implicitly convert type `vicoapi.Pictures' to `System.Collections.Generic.List<vicoapi.Pictures>' (CS0029)

Hello Together following my Code:
public class SampleData : DropCreateDatabaseIfModelChanges<VicoTvEntities>
{
protected override void Seed(VicoTvEntities context)
{
var pictures = new List<Pictures>{
new Pictures { Name = "Testbild", Url="localhost/test" }
};
var user = new List<User>{
new User{Username="Muster", Password="Pass",Email="max.muster#mustermail.com",Bio="Musterbiografie", Pictures = pictures.Find(pic => pic.Name == "Testbild")}
};
}
}
}
I try to build an API and at the moment, I work on the connection of the DB to the code. I'm following this tutorial: https://www.asp.net/mvc/overview/older-versions/mvc-music-store/mvc-music-store-part-4
My Problem is, whenever I try to add the Picture it won't work, because of this converting error. How can I avoid this problem and implement the picture to the User.
My Source Tree looks like this:
Source Tree
The User Class looks as following:
public class User
{
public int IdUser { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string Email { get; set; }
public string Bio { get; set; }
public List<Pictures> Pictures { get; set; }
public List<Follow> Following { get; set; }
public List<Follow> Followed { get; set; }
}
The Error I get looks like this:
/Users/username/vicotv-Backend/vicoapi/vicoapi/Models/SampleData.cs(126,126): Error CS0029: Cannot implicitly convert type `vicoapi.Pictures'to System.Collections.Generic.List' (CS0029) (vicoapi)
Although you don't show the class here, I'm fairly certain that your User class has a property defined something like this:
public List<Pictures> Pictures { get; set; }
If that is the case, you need to assign the property to the whole list like this:
user.Pictures = pictures
If you want a user to only be able to have one picture then you need to define the Picture property on the User class like this:
public Pictures Picture { get; set; }
If you define the Picture property like this it should work with your current code.
P.S. I would definitely recommend renaming your "Pictures" class to "Picture". In general you should use the singular form for class names. Have a look at this page for more information.

best way to exclude some parameters in a modelview when editing a page

I know there are a couple of options to exclude/include some parameters in a modelview like using bind or using interfaces. However I have some problems when I am trying to implement nested IEnumerable variables. For example:
public class TestViewModel()
{
public int Id { get; set; }
public IEnumerable<Organisation> KPI { get; set; }
}
public class Organisation
{
public string Id { get; set; }
public string Name {get; set;}
public DateTime StartDate {get; set;}
public IEnumerable<Regiod> CategoryValues { get; set; }
}
public class Region
{
public System.Guid Id { get; set; }
public System.Int32 RegionId { get; set; }
public int Value { get; set; }
public System.String RegionName { get; set; }
}
[HttpGet]
public ActionResult edit(int id)
{
var model = new TestViewModel();
// Do something to populate the model
view(model)
}
In the view page (razor) all fields are disabled or hidden, except field Value in Region class and StartDate in Organization. My action Code is something like:
[HttpPost]
public ActionResult edit(TestViewModel model)
{
// Do something to populate the model
}
Everything works fine, unless somebody uses for example fiddler to set other disabled or hidden values, so those fields will be updated.
What I am after is to update just enabled fields and exclude the rest even somebody tries to set a value for them.
I tried bind[Exclude and Include], but my problem is I can bind 2 values from different classes. I tried UpdateModel(model, include) and it didn't work.
Any advice would be appreciated.

Populate DropDown from database in an edit view using MVC4

I am new to MVC and trying to populate a dropdown list in the "create" view which is generated from a view model, but it returns with an error saying object reference is not an instance of an object. below is my code :
Controller Code:
public ActionResult Create()
{
return View(new AddRecipeViewModel());
}
Model Code:
public class DifficultyLevel
{
[Key]
public int Id { get; set; }
public string Difficulty { get; set; }
}
public class AddRecipeViewModel
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<RecipeReview> Reviews { get; set; }
public virtual IEnumerable<DifficultyLevel> Difficulty { get; set; }
}
View:
<div>
<select>
#foreach (var item in Model.Difficulty)
{
<option>#item.Difficulty</option>
}
</select>
</div>
Is there an easy workaround this ? as I will be adding more drop downs in this as I go along.
Thanks,
Vishal
not sure if you need to use virtual in your view models.. that's usually only for the entity models. but anyway, try adding a constructor to AddRecipeViewModel and set the collections equal to empty lists so they won't be null.
public class AddRecipeViewModel
{
public AddRecipeViewModel()
{
Reviews = new List<RecipeReview>();
Difficulty = new List<DifficultyLevel>();
}
[Key]
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<RecipeReview> Reviews { get; set; }
public virtual IEnumerable<DifficultyLevel> Difficulty { get; set; }
}

MVC Radio Button List Grid

I've got an issue trying to read the selected values from the radio button list in my [HttpPost] Controller. Any help would be greatly appreciated.
My models are the following:
public partial class RoleModel
{
public Guid RoleId { get; set; }
public string Description { get; set; }
public List<RoleModuleAccessRight> RoleModuleAccessRights { get; set; }
}
public class RoleModuleAccessRight
{
public string ModuleName { get; set; }
public int ModuleId { get; set; }
public bool HasFullControl { get; set; }
public bool HasReadOnly { get; set; }
public bool HasNoAccess { get; set; }
}
My Controllers:
[HttpGet]
public ActionResult Edit(Guid id)
{
RoleModel role = BusinessLayer.UserManager.GetRoleModel(id);
role.RoleModuleAccessRights = BusinessLayer.UserManager.GetModulesForRoleId(role.RoleId);
return PartialView("_Edit", role);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(RoleModel model)
{
if (ModelState.IsValid)
{
BusinessLayer.UserManager.UpdateAccessRights(model);
string url = Url.Action("List", "Roles");
return Json(new { success = true, url = url });
}
return PartialView("_Edit", model);
}
My View code:
#foreach (RoleModuleAccessRight item in Model.RoleModuleAccessRights)
{
#Html.HiddenFor(model => item.ModuleId)
string radioName = string.Format("RoleModuleAccessRights_{0}", item.ModuleId.ToString());
<tr>
<td>#item.ModuleName</td>
<td>
<div class="checkbox">
#Html.RadioButton(radioName, item.HasFullControl, item.HasFullControl)
</div>
</td>
<td>
<div class="checkbox">
#Html.RadioButton(radioName, item.HasReadOnly, item.HasReadOnly)
</div>
</td>
<td>
<div class="checkbox">
#Html.RadioButton(radioName, item.HasNoAccess, item.HasNoAccess)
</div>
</td>
</tr>
}
The issue im having is that when i post the form im not able to grab the information in my [HttpPost] Controller. It returns "null"
The mark up generated is the following:
Looking at your code, your ids are not unique which is going to break things, also using a dedicated template will simplify your problem. See this example https://stackoverflow.com/a/7668325
Another more generic article: Multiple radio button groups in MVC 4 Razor