Home How to set a non-string value taken from a SelectList to a Model inside a form?
Reply: 1

How to set a non-string value taken from a SelectList to a Model inside a form?

user1913744
1#
user1913744 Published in 2018-02-11 20:42:20Z

I have a Model which i want to edit (Location).This model has a field called ActivityId.I am sending an array of ActivityId-s via ViewData to the view and transform them to a SelectList. I want the ActivityId field (long) of the Location to be set with the selected item from the dropdownlist (string) - i need a conversion done somehow from string to long before entering the Post Action of the controller.(EditConfirmed)

Models:

 [Table("location")]
 public partial class Location
    {
        public Location()
        {
            StoryLocation = new HashSet<StoryLocation>();
            UserStoryLocation = new HashSet<UserStoryLocation>();
        }

        [Column("id", TypeName = "bigint(20)")]
        public long Id { get; set; }
        [Column("description")]
        [StringLength(255)]
        public string Description { get; set; }
        [Column("name")]
        [StringLength(255)]
        public string Name { get; set; }


        [Column("activity_id", TypeName = "bigint(20)")]
        public long? ActivityId { get; set; }
        [ForeignKey("ActivityId")]
        [InverseProperty("Location")]
        public Activity Activity { get; set; }
       }
    }




 [Table("activity")]
    public partial class Activity
    {
        public Activity()
        {
            Location = new HashSet<Location>();
        }

        [Column("activity_id", TypeName = "bigint(20)")]
        public long ActivityId { get; set; }
        [Column("description")]
        [StringLength(255)]
        public string Description { get; set; }
        [Column("name")]
        [StringLength(255)]
        public string Name { get; set; }
        [Column("type")]
        [StringLength(255)]
        public string Type { get; set; }

        [InverseProperty("Activity")]
        public ICollection<Location> Location { get; set; }
    }

Controller:

    [HttpGet]
            public IActionResult Edit(long id = 0)
            {

                Location loc = this.context.Locations.Find(id);


                if (loc == null)
                {
                    return NotFound();
                }
                ViewData[Constants.ViewData.TActivities]=this.context.Activities
                                .Select(elem=>
                                       new SelectListItem
                                          {
                                          Text=elem.Name,
                                          Value=elem.ActivityId.ToString()
                                          }
                                       ).ToList();
                return View(loc);
            }

View

@using AdminMVC.Models
@using AdminMVC.ConfigConstants
@using Newtonsoft.Json
@model AdminMVC.Models.Location

@{
    List<SelectListItem> dropActivities=ViewData[Constants.ViewData.TActivities] as List<SelectListItem>;
}

<html>
    <head>
    </head>
    <body>
        <div id="form">

        </div>
        <div id="page">
        @using (Html.BeginForm("Edit","Location",FormMethod.Post))
        {
            <div id="table">
            <label>Set Location:</label>
            <table border="">

               @Html.DisplayFor(x=>x.Id)
            <tr>
                <td>@Html.DisplayNameFor(x=>x.Name)</td>
                <td>@Html.EditorFor(x=>x.Name)</td>
            </tr>
            <tr>
                <td>@Html.DisplayNameFor(x=>x.Description)</td>
                <td>@Html.EditorFor(x=>x.Description)</td>
            </tr>

            <div>
            <label >Select Activity:</label>
             @Html.DropDownList("Activity",dropActivities) //i need to somehow convert the selected value from the dropdown to long before the form is sent to the controller
            </div>
             </table>
             </div>
             <div id="control-panel">
            <input type="submit" value="Edit">
            </div>
        }
    </body>
    </div>
</html>

Post to Controller

[HttpPost, ActionName("Edit")]
            public IActionResult EditConfirmed(Location editLoc) 

            {
                if (ModelState.IsValid)
                {
                    this.context.Entry(editLoc).State = EntityState.Modified;
                    this.context.SaveChanges();
                    return RedirectToAction("Index");
                }
                return View(editLoc);
            }

P.S:So far the ActivityId of the Location sent to the Post Action is null.I need it to be long.

user1913744
2#
user1913744 Reply to 2018-02-25 10:23:39Z

I solved this problem using the ViewData component.I would first serialize the SelectList using NewtonSoft then i would add it to the ViewData dictionary.When rendering the view i would use the DropDownList razor Html Helper method.

Model

public class FixedLocation
{
    [Column("id", TypeName = "bigint(20)")]
    public long Id { get; set; }
    [Column("coords")]
    [StringLength(255)]
    public string Coords { get; set; }
    [Column("name")]
    [StringLength(255)]
    [Required]
    public string Name { get; set; }
    [Column("google_id")]
    [StringLength(255)]
    [Required]
    public string GoogleId { get; set; }
}

Extension method for getting a List of SelectListItem

public static IEnumerable<SelectListItem>  ToSelectList<T,Tkey,Tvalue>(
                                         this IQueryable<T> myenum,
                                         Func<T,(Tkey,Tvalue)>kvpair)
                 {
                   return myenum.Select(elem=>kvpair(elem))
                                .Select(tuple=>new SelectListItem{                                                                          
                                                 Text=tuple.Item1.ToString(),                                                  
                                                 Value=tuple.Item2.ToString()
                                                                });
                 }

Controller:

public IActionResult Create()
        {
            Location targetLocation = new Location();                
            ViewData[Constants.ViewData.TFixedLocations]=
                                this.context.FixedLocation
                                .ToSelectList<FixedLocation,string,long>
                                 (elem=>(elem.Name,elem.Id)).ToList();

            return View(targetLocation);
        }

View:

    @using AdminMVC.Models
    @model AdminMVC.Models.Location
    @using AdminMVC.ConfigConstants

    @{
      dropFixedLocations=ViewData[Constants.ViewData.TFixedLocations] as List<SelectListItem>;
    }
                <div>
                <label >Select FixedLocation:</label>
                 @Html.DropDownListFor(x=>Model.Id,dropFixedLocations)
                </div>
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.303238 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO