Cannot convert type 'char' to 'string - asp.net-mvc

I am getting this error in one project and the code works perfectly fine on another project. I have tried multiple to duplicate the code as it is. Is there a way to find where the error originated from??
#if (ViewBag.RolesForThisUser != null)
{
<div style="background-color:lawngreen;">
<table class="table">
<tr>
<th>
#Html.DisplayName("Roles For This User")
</th>
</tr>
<tr>
<td>
#foreach (string s in ViewBag.RolesForThisUser) //this line
{
<li>#s</li>
}
</td>
</tr>
</table>

I suspected ViewBag.RolesForThisUser itself already contains a string, neither an array nor collection of strings (e.g. string[] or List<string>), hence using foreach loop is pointless (and string itself contains char[] array which explains why type conversion failed). You can simply display it without foreach:
#if (!string.IsNullOrEmpty(ViewBag.RolesForThisUser))
{
<div style="background-color:lawngreen;">
<table class="table">
<tr>
<th>
#Html.DisplayName("Roles For This User")
</th>
</tr>
<tr>
<td>
#ViewBag.RolesForThisUser
</td>
</tr>
</table>
</div>
}
Or assign a string collection to ViewBag.RolesForThisUser from GET method so that you can use foreach loop, like example below:
Controller
public ActionResult ActionName()
{
var list = new List<string>();
list.Add("Administrator");
// add other values here
ViewBag.RolesForThisUser = list;
return View();
}
View
#if (ViewBag.RolesForThisUser != null)
{
<div style="background-color:lawngreen;">
<table class="table">
<tr>
<th>
#Html.DisplayName("Roles For This User")
</th>
</tr>
<tr>
<td>
#foreach (string s in ViewBag.RolesForThisUser)
{
<p>#s</p>
}
</td>
</tr>
</table>
</div>
}

Related

how to pass two values in a form inside a table row to the controller ? Razor

How can I make this table
<table class="table table-bordered table-striped" style="width:100%">
<thead>
<tr>
<th>
Name
</th>
<th>
Price
</th>
<th>
Quantity
</th>
<th>
Options
</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model.Items)
{
<tr>
<td>#item.Name</td>
<td>#item.Price</td>
<td><input type="number" name="Quantity"/> </td>
<td><input type="submit" class="btn btn-primary" value="Add" /></td>
</tr>
}
</tbody>
</table>
To work like the table below (this one works fine)
<table class="table table-bordered table-striped" style="width:100%" >
<thead>
<tr>
<th>
Name
</th>
<th>
Prrice
</th>
<th>
Quantity
</th>
<th class="text-center">
Options
</th>
</tr>
</thead>
<tbody>
#foreach(var item in Model)
{
<tr>
<td width="55%">#item.Name</td>
<td width="10%" class="text-right">#item.Price $</td>
<td width="10%" class="text-right">#item.Quantity</td>
<td width="25%">
<div class="w-100 btn-group"role="group">
<a asp-controller="item" asp-action="Update" asp- asp-route-Id="#item.Id" class="btn btn-outline-primary mx-1">Update</a>
<a asp-controller="item" asp-action="Delete" asp-route-Id="#item.Id" class="btn btn-outline-danger mx-1">Delete</a>
</div>
</td>
</tr>
}
</tbody>
</table>
I want to pass the item.quantity and item.id to the controller.
Item model
public class Item
{
public int Id { get; set; }
public string Name { get; set; }
public float Price { get; set; }
public int Quantity { get; set; }
}
I found that you can put each item in a table and a form for each one Post, but I would like to know if with asp.net it can be made simpler
If you need to post ALL ROW VALUES (assuming each row is a different object) then use a form outside the table and take needed properties into inputs in order to get posted.
If you need to post SINGLE ROW VALUES (only submit the row where the value is beign modified) then you could write some JS and make a post or ajax post of all inputs in the current row.
$(document).on('keypress', 'input[data-ajax="row"]', function (e) {
if (e.keyCode !== 13) {
return;
}
var self = $(this);
var row = self.closest('tr');
var url = '/controller/action';
var propertyNames = new Array();
var propertyValues = new Array();
row.find('input[data-ajax="row"]').each(function () {
$(this).attr('readonly', 'readonly');
propertyNames.push($(this).attr('id'));
propertyValues.push($(this).val());
});
$.ajax({
url: url,
type: 'POST',
data: { propertyNames, propertyValues },
success: function (data) {
//SUCCESS CODE HERE (reload)
},
error: function (data) {
console.log('error');
alert('error');
}
});
return;
});
Note that you will need to add data-ajax="row" attribute to your input. Alsou you could remove the dynamic code and find all needed controls inside the row scope.
Ej: var name = row.find('#name').val()

Use single IEnumerable value before For Each

I have a view that lists auditorium names of a theater.
#ModelType IEnumerable(Of dbtheatersinfo.TheaterAuditorium)
<h2>Theater Auditoriums</h2>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(Function(model) model.Theater.TheaterName)
</th>
<th>
#Html.DisplayNameFor(Function(model) model.TheaterAuditoriumName)
</th>
</tr>
#For Each item In Model
#<tr>
<td>
#Html.DisplayFor(Function(modelItem) item.Theater.TheaterName)
</td>
<td>
#Html.DisplayFor(Function(modelItem) item.TheaterAuditoriumName)
</td>
</tr>
Next
</table>
All auditoriums listed here have the same TheaterName, so I would like to display the first instance of TheaterName in the tag (and remove it from the list). I tried:
<h2>Auditoriums for #Html.DisplayFor(Function(model) model.Theater.TheaterName) </h2>
But that gives me, "'Theater' is not a member of 'IEnumerable(of TheaterAuditorium)'." The data is in there; it's displaying within the For Each loop. I just can't figure out how to use it before the loop.
I asked the wrong question, but ended up needing the answer anyway. To do this View properly, I needed a View Model:
Namespace ViewModels
Public Class AuditoriumIndex
Public Property TheaterAuditorium As IEnumerable(Of TheaterAuditorium)
Public Property Theater As Theater
End Class
End Namespace
In the controller:
Dim viewModel = New AuditoriumIndex()
viewModel.TheaterAuditorium = db.TheaterAuditoriums.Where(Function(a) a.TheaterID = id).SortBy("TheaterAuditoriumName")
viewModel.Theater = db.Theaters.Where(Function(a) a.TheaterID = id).SingleOrDefault()
Return View(viewModel)
And the View:
h2>Auditoriums for #Html.DisplayFor(Function(model) model.Theater.TheaterName)</h2>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(Function(model) model.TheaterAuditorium.FirstOrDefault().TheaterAuditoriumName)
</th>
</tr>
#For Each item In Model.TheaterAuditorium
#<tr>
<td>
#Html.DisplayFor(Function(modelItem) item.TheaterAuditoriumName)
</td>
</tr>
Next
</table>
Here I had to use FirstOrDefault() to access the column name outside of the loop.

MVC Object(x) does not contain a definition for blah

Getting an error on the view, at the displaynamefor softwareid line, saying the model SoftwareDTO does not contain a definition for softwareid. I can see it right there in the model.
Model:
public class SoftwareDTO
{
public int SoftwareId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
Controller:
public ActionResult Index()
{
List<SoftwareDTO> softwareList = new List<SoftwareDTO>();
var data = _db.Software.ToList();
foreach (var sw in data)
{
SoftwareDTO software = new SoftwareDTO()
{
SoftwareId = sw.SoftwareId,
Name = sw.Name,
Description = sw.Description
};
softwareList.Add(software);
};
return View(softwareList);
}
View:
#model List<Request.Models.SoftwareDTO>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.SoftwareId)
</th>
<th>
#Html.DisplayNameFor(model => model.Name)
</th>
<th>
#Html.DisplayNameFor(model => model.Description)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.SoftwareId)
</td>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.Description)
</td>
its because model its a list not an object SoftwareDTO in your razor view
I think you are missing the foreach
SoftwareId is a property of SoftwareDTO class. Your view is strongly typed to a collection of SoftwareDTO objects. So you need to loop through the model(The collection of SoftwareDTO) and access the SoftwareId of each item.
#model List<Request.Models.SoftwareDTO>
<table class="table">
#foreach(var item in Model)
{
<tr>
<td>
#Html.DisplayNameFor(x=> item.SoftwareId)
</td>
</tr>
}
</table>
EDIT : As per the edit in the question, and the comments provided.
Looks like you want to print the display name of the propertes in your table headers. If you do not wish to change the data you are passing from your action method, you can try this
#if (Model.Any())
{
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(x => Model[0].SoftwareId)
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(x => item.SoftwareId)
</td>
</tr>
}
</table>
}
This is using the first item in the collection and it's properties to use with DisplayNameFor method. Since i have a if condition to check for at least one item before rendering the table, It will not even render the table if your Model has 0 items.
If you want to show the empty table with headers, you have 2 options.
Write HTML markup for the table header
<table class="table">
<tr>
<th>
<label>Software Id</label>
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(x => item.SoftwareId)
</td>
</tr>
}
</table>
Or if you still want to use the DisplayNameFor helper method to render the table header labels,
Create a new viewmodel
public class TableListVm
{
public List<SoftwareDTO> Items {set;get;}
public SoftwareDto ItemMeta {set;get;}
public TableListVm()
{
ItemMeta= new SoftwareDto();
}
}
And in your GET action, Send this object to your view
public ActionResult Index()
{
var data = _db.Software.ToList().Select(sw=> new SoftwareDTO {
SoftwareId = sw.SoftwareId,
Name = sw.Name,
Description = sw.Description
}).ToList();
var vm= new TableListVm { Items = data };
return View(vm);
}
And in your view which is strongly typed to this new view model.
#model TableListVm
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(x => Model.ItemMeta.SoftwareId)
</th>
</tr>
#foreach (var item in Model.Items)
{
<tr>
<td>
#Html.DisplayFor(x => item.SoftwareId)
</td>
</tr>
}
</table>

bind a table on click event of button

I am working with ASP.NET MVC 3 Razor. I have a list method in controller, so want to bind this list to table on click event of a button. How will I achieve this functionality.
Here is my controller method:
public ActionResult getItnList(string scanCode)
{
List<List<String>> getitnDetails_List = getitnDetails(date, name);
ViewBag.getitnDetails = getitnDetails_List;
return Json(new { getitnDetails_List = getitnDetails_List }, JsonRequestBehavior. AllowGet);
}
Here is my view code:
#{
List<List<String>> str = (List<List<String>>)ViewBag.getitnDetails;
}
<table id="list" width="100%">
<td><b>Harvest Date</b></td>
<td><b>Product</b></td>
#for (int i = 0; i <= str.Count - 1; i++)
<td>#str[i][0].ToString()</td>
<td>#str[i][1].ToString()</td>
</table>
How will I bind this table to list on click event of button?
I'm not exactly sure what you are asking. But if you want to get the list on the click of a button you can use jQuery .getJSON (http://api.jquery.com/jQuery.getJSON/) to retrieve the list data and then insert it with javascript. Or you could render the list in the action and simply use json .get and insert the result.
Try this
View
<table>
<tr>
<th>
Name
</th>
<th>
PhoneNo
</th>
<th>
Address
</th>
<th>
Action
</th>
</tr>
#{var list = ViewBag.RegisterItems; }
#if (list != null)
{
foreach (var m in list)
{
<tr>
<td>
<input id="txtName-#m.ID.ToString()" type="text" class="hide" value="#m.Name.ToString()"/>
</td>
<td>
<input id="txtPhoneNo-#m.ID.ToString()" type="text" class="hide" value="#m.PhoneNo.ToString()"/>
</td>
<td>
<input id="txtAddress-#m.ID.ToString()" type="text" class="hide" value="#m.Address.ToString()"/>
</td>
</tr>
}
}
</table>
Controller
public ActionResult CustomerInfo()
{
ViewBag.RegisterItems = GetAllRegisterData();
return View();
}

How to display list items in view passed from the controller ?

I am developing MVC app.
I want to create the list in the controller and pass it to the view.
I have written the method in the controller but dont know how to call it form view and display the value which it return.
Method in controller.
public List<Invoice> GetInvoiceList(int Pid)
{
List<Invoice> Invoices = new List<Invoice>();
var InvoiceList = (from i in db.Invoices
where i.PaymentAdviceId == Pid
select i);
Invoices = InvoiceList.ToList();
return (Invoices);
}
View Code
<div class="row-fluid">
<table class="table table-striped table-hover">
<thead>
<tr>
<th>Advice No
</th>
<th>
Invoices
</th>
</tr>
</thead>
#foreach (var item in Model)
{
<tbody>
<tr>
<td>
#Html.DisplayFor(modelItem => item.AdviceNo)
</td>
I wan to call the controller method GetInvoiceList here and
want to display list items here...
<td>
</tr>
</tbody>
Add a PartialView to your project that has it's model set to List<Invoice>
then modify your code:
public PartialViewResult GetInvoiceList(int Pid)
{
List<Invoice> Invoices = new List<Invoice>();
var InvoiceList = (from i in db.Invoices
where i.PaymentAdviceId == Pid
select i);
Invoices = InvoiceList.ToList();
return PartialView("partialViewName", Invoices);
}
in your other view:
<tr>
<td>
#Html.DisplayFor(modelItem => item.AdviceNo)
</td>
<td> #Html.Action("GetInvoiceList", new {Pid = item.id})</td>
</tr>

Resources