-1

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.

Bercovici Adrian
  • 8,794
  • 17
  • 73
  • 152
  • 1
    Show your model! Your binding to a property named `Activity` (not `ActivityId`) and if `Activity` is `long` it will be correctly bound –  Feb 11 '18 at 20:49
  • I added it now. – Bercovici Adrian Feb 11 '18 at 21:27
  • 1
    Then its `@Html.DropDownList("ActivityId", dropActivities)` - but I suggest your read [this Q/A](https://stackoverflow.com/questions/34366305/the-viewdata-item-that-has-the-key-xxx-is-of-type-system-int32-but-must-be-o) to understand how to do this correctly –  Feb 11 '18 at 21:29
  • Thank you very much setting the first parameter to ActivityId did the trick.I thought that the SelectList only takes the Text and Value parameters and that's it. – Bercovici Adrian Feb 11 '18 at 21:34
  • 1
    It does - but you binding the selected value to `Activity` which is a complex object - a ` –  Feb 11 '18 at 21:40

1 Answers1

0

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>
Bercovici Adrian
  • 8,794
  • 17
  • 73
  • 152