Setting Style for ListBox in MVC Razor - razor

This is Razor Syntax for ListBox i am using in my page. How can i set its height and width ?
Here noCategories is View data coming from Controller.
$
#Html.ListBox("noCategories");
Controller:
public ActionResult Configure(int? nopCategoryid)
{
model = new DataImporttModel();
DisplaynopCommerceCategories(model, nopCategoryid);
return View("Nop.Plugin.Data.Import.Views.DataImport.Configure", model);
}
private void DisplaynopCommerceCategories(DataImporttModel model, int? nopCategoryid)
{
var _categoryservice = new NopEngine().Resolve<ICategoryService>();
MultiSelectList sl;
model.nopCommerceCategories = new List<CS_ListItems>();
foreach (var item in _categoryservice.GetAllCategories().ToList())
{
model.nopCommerceCategories.Add(new CS_ListItems() { Name = item.Name, ID = item.Id });
}
if (nopCategoryid != null)
{
sl = new MultiSelectList(model.nopCommerceCategories, "ID", "Name", new[] { nopCategoryid });
}
else
{
sl = new MultiSelectList(model.nopCommerceCategories, "ID", "Name");
}
ViewData["nopCommerceCategories"] = sl;
}
public class DataImporttModel
{
public List<C_Category> Mappings { get; set; }
public List<CS_ListItems> ClockCategories { get; set; }
public List<CS_ListItems> nopCommerceCategories { get; set; }
}

You can apply a class to it and then style it through CSS:
#Html.ListBox("noCategories", ViewData["nopCommerceCategories"] as MultiSelectList, new {#class = "mylistbox"});
CSS
.myclass{
width: 100px;
}
Alternatively, you can style it inline:
#Html.ListBox("noCategories", ViewData["nopCommerceCategories"] as MultiSelectList, new {#style = "width: 100px;"});

Related

Blazor Telerik Grid set cell color from data

Trying to set cell background color based on hex code from dataset. Not seeing a property for the grid itself so I am trying binging style to the object Course.ColorHex value for each row with some sort of property binding.
Course Object
public class Course
{
public int Id { get; set; }
public string CourseTitle { get; set; }
public string CourseDescription { get; set; }
public string ColorHex { get; set; }
}
Controller
[ApiController]
[Route("[controller]")]
public class CourseController : ControllerBase
{
private static readonly string[] colorList = new[] { "#ff0000", "##ffff00", "#00ff00", "#00ffff", "#0000ff", "#ff00ff" };
[HttpGet]
public IEnumerable<Course> GetCourses()
{
var courses = new List<Course>();
for (var i = 0; i < colorList.Length; i++)
{
var idCount = i + 1;
var course = new Course()
{
Id = idCount,
CourseTitle = $"Course {idCount}",
CourseDescription = $"Course {idCount} description",
ColorHex = colorList[i]
};
courses.Add(course);
}
var result = courses.ToArray();
return result;
}
}
Blazor
#page "/grid"
#using TelerikGridColorTest.Shared
#inject HttpClient Http
<h3>Courses</h3>
<TelerikGrid Data="courses">
<GridColumns>
<GridColumn Field="#(nameof(Course.CourseTitle))" />
<GridColumn Field="#(nameof(Course.CourseDescription))" />
<GridColumn>
<Template>
<div style="background-color: #(nameof(Course.ColorHex)); padding: 10px;"></div>
</Template>
</GridColumn>
</GridColumns>
</TelerikGrid>
#code {
Course[] courses { get; set; }
protected override async Task OnInitializedAsync()
{
courses = await Http.GetFromJsonAsync<Course[]>("Course");
}
}
Figured it out. Forgot to one address the field I was working with in the Field property.
#page "/grid"
#using TelerikGridColorTest.Shared
#inject HttpClient Http
<h3>Courses</h3>
<TelerikGrid Data="courses">
<GridColumns>
<GridColumn Field="#(nameof(Course.CourseTitle))" />
<GridColumn Field="#(nameof(Course.CourseDescription))" />
<GridColumn Field="#(nameof(Course.ColorHex))">
<Template>
#{
var color = context as Course;
<div style="background-color: #(color.ColorHex); padding: 10px;"></div>
}
</Template>
</GridColumn>
</GridColumns>
</TelerikGrid>
#code {
Course[] courses { get; set; }
protected override async Task OnInitializedAsync()
{
courses = await Http.GetFromJsonAsync<Course[]>("Course");
}
}

CsvHelper mapping issue

I have a question related to CsvHelper library and I cannot figure out how to map a csv file to the following MyClass object.
the csv file look like:
id, Property1, property2....
1, x, a,b,c
2,x, d,f
3, y, g,h,i,j,k
...
Which I would like to parse to the following classes
public class MyClass
{
public string Id { get; set; }
public List<Custom> MyCustoms { get; set; }
}
public class Custom
{
public string Property1 { get; set; }
public List<string> Property2 { get; set; }
}
ConvertUsing is going to be the easiest way to map this.
public class Program
{
static void Main(string[] args)
{
using (var stream = new MemoryStream())
using (var writer = new StreamWriter(stream))
using (var reader = new StreamReader(stream))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
writer.WriteLine("Id,Property1,Property2,Property3,Property4,Property5,Property6");
writer.WriteLine("1,x,a,b,c");
writer.WriteLine("2,x,d,f");
writer.WriteLine("3,y,g,h,i,j,k");
writer.Flush();
stream.Position = 0;
var test = csv.Configuration.RegisterClassMap<MyClassMap>();
var records = csv.GetRecords<MyClass>().ToList();
}
}
}
public class MyClassMap: ClassMap<MyClass>
{
public MyClassMap()
{
Map(m => m.Id);
Map(m => m.MyCustoms).ConvertUsing(row =>
{
var custom = new Custom
{
Property1 = row["Property1"],
Property2 = new List<string>()
};
var index = 2;
while (row.TryGetField(typeof(string), index, out var property))
{
custom.Property2.Add((string)property);
index++;
}
return new List<Custom> { custom };
});
}
}
public class MyClass
{
public string Id { get; set; }
public List<Custom> MyCustoms { get; set; }
}
public class Custom
{
public string Property1 { get; set; }
public List<string> Property2 { get; set; }
}

MVC4 Razor how to get selected value from dropdownlist?

My Model:
namespace mvc4deneme.Models
{
public class SiparisModel
{
public string SelectedValue { get; set; }
public IEnumerable<SelectListItem> Values { get; set; }
[Display(Name = "Ürün Tipi")]
public Material UrunTipi { get; set; }
[Display(Name = "Yüzey")]
public Material Yuzey { get; set; }
public class Material
{
//public int? ID { get; set; }
public string ID { get; set; }
public string Name { get; set; }
}
}
MY CONTROLLER:
//my control page is SiparisController
public class SiparisController : Controller
{
//
// GET: /Siparis/
public ActionResult Index()
{
DataTable dt = null;
Material material = null;
var model = new SiparisModel();
List<Material> lstYuzey = new List<Material>{
new Material { ID = null, Name = "Seçiniz" },
new Material { ID = "D/-", Name = "Tek Yüz" },
new Material { ID = "D/D", Name = "Çift Yüz" } };
ViewBag.Yuzey = lstYuzey;
List<Material> lstUrunTipi = new List<Material> {
new Material { ID = null, Name = "Seçiniz" },
new Material { ID = "3100140", Name = "MKYL" },
new Material { ID = "3100180", Name = "FS.MKYL FA" },
new Material { ID = "3100190", Name = "FS.MKYL FC" },
new Material { ID = "3100090", Name = "YL" }, };
ViewBag.UrunTipi = lstUrunTipi;
return View(model);
}
}
MY Index.cshtml view page:
#using (Html.BeginForm())
{
#Html.DropDownListFor(x => x.SelectedValue, Model.Values)
<button type="submit">OK</button>
#Html.LabelFor(m => m.UrunTipi)
#Html.DropDownListFor(m => m.UrunTipi.ID,
new SelectList(ViewBag.UrunTipi, "ID", "Name"),
new { style = "width:310px" })
#Html.ValidationMessageFor(m => m.UrunTipi.ID) }
#using (Html.BeginForm())
{
#Html.LabelFor(m => m.Yuzey)
#Html.DropDownListFor(m => m.Yuzey.ID,
new SelectList( ViewBag.Yuzey,"ID", "Name"),
new { style = "width:310px" })
#Html.ValidationMessageFor(m => m.Yuzey.ID)
}
My Ask:
How can i execute ok button to getting values of my UrunTipi and Yuzey selected values.
Thank you so much.
Regards,
B.Y.

How to turn a simple class into a KVC for binding to a CollectionViewItem

I have a simple class
public class Colora
{
public Colora()
{
Created = DateTime.Now;
}
public string Name { get; set; }
public DateTime Created { get; set; }
}
that needs to stay simple (it is in a PCL). How do I turn this into a KVC compliant NSObject so that it can be displayed in a CollectionView?
I was thinking something like
List<Colora> coloras = new List<Colora> () {
new Colora() { Name = "Red" },
new Colora() { Name = "Blue" }
};
var objs = new NSObject[coloras.Count];
for (int i = 0; i < coloras.Count; i++) {
objs [i] = NSObject.FromObject (coloras [i]);
}
devCollectionView.Content = objs;
but FromObject does not work with my class. In this case the array does not have to be mutable.
Update. Remarkably, this works
public class ColoraViewModel : NSObject
{
private readonly Colora _colora;
public ColoraViewModel (Colora colora)
{
_colora = colora;
}
[Export("name")]
public string Name {
get { return _colora.Name; }
set {
if (_colora.Name != value) {
_colora.Name = value;
}
}
}
}
and
List<Colora> coloras = new List<Colora> () {
new Colora() { Name = "Red" },
new Colora() { Name = "Blue" }
};
var objs = new NSObject[coloras.Count];
for (int i = 0; i < coloras.Count; i++) {
objs [i] = new ColoraViewModel (coloras [i]);
}
devCollectionView.Content = objs;
though I'm still looking for a general way to create the KVC without having to make a new ViewModel.
This doesn't work, but I dream of something like this:
public class KVCObject : NSObject
{
private readonly object _t;
private readonly Type _type;
public KVCObject (object t)
{
_t = t;
_type = t.GetType();
}
public override void SetValueForKey (NSObject value, NSString key)
{
var info = _type.GetProperty (key.ToString ());
info.SetValue (_t, value);
}
public override NSObject ValueForKey (NSString key)
{
var info = _type.GetProperty (key.ToString ());
return NSObject.FromObject(info.GetValue (_t));
}
}

MVC3 with JsonFilterAttribute doesn't work anymore

This code was working just fine before i moved to MVC3 ...
[HttpPost]
[ActionName("GetCommentListForServiceCall")]
[UrlRoute(Path = "mobile/servicecalls/{id}/comments", Order=1)]
[UrlRouteParameterConstraint(Name = "id", Regex = #"\d+")]
[OutputCache(CacheProfile = "MobileCacheProfile")]
[JsonFilter(JsonDataType = typeof(ServiceCallCommentNewDTO), Param = "comment")]
public JsonNetResult CreateCommentForServiceCall(int id, ServiceCallCommentNewDTO comment)
{
ServiceCallComment entity = coreSvc.SaveServiceCallComment(id, 0, comment.CategoryId, comment.Comment);
SetResponseCode(HttpStatusCode.Created);
SetContentLocation(string.Format("mobile/servicecalls/{0}/comments/{1}", id.ToString(), entity.Id.ToString()));
return new JsonNetResult(entity); ;
}
here is the JSonFilterAttribute code
public class JsonFilterAttribute : ActionFilterAttribute
{
public string Param { get; set; }
public Type JsonDataType { get; set; }
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext.HttpContext.Request.ContentType.Contains("application/json"))
{
string inputContent;
using (var sr = new StreamReader(filterContext.HttpContext.Request.InputStream))
{
inputContent = sr.ReadToEnd();
}
var result = JsonConvert.DeserializeObject(inputContent, JsonDataType, new JsonSerializerSettings{TypeNameHandling = TypeNameHandling.All});
filterContext.ActionParameters[Param] = result;
}
}
}
Now The JsonFilter doesn't get the object anymore. It always return null ?
Is there something i have to do in MVC3 ?
You no longer need this attribute as ASP.NET MVC 3 has this functionality built-in.