How to get the View to return the ID via a ViewMod

2019-07-28 21:39发布

问题:

I currently have a view which returns an IEnumerable collection of Technicians via the TechnicianViewModel. The view model populates fine and displays the objects correctly.

However, at the moment I need someone to point me in the right direction with regards to selecting a specific technician.

If I was to click More Info on the technician, an [HttpGet] request would be initiated which would result in a queryString. And this is exactly what is against the requirements of this project.(Requirements of the project: no query string should appear in the URL)

Here is the view :-

  @model IEnumerable<ActionAugerMVC.ViewModels.TechnicianViewModel>
  <div class="row">
        @foreach (var item in Model)
        {
            <div class="col-sm-6 col-md-3">
                <div class="">
                    <div class="">
                        <div class="">
                           @item.UserName
                        </div>
                        <div class="">
                            Technician
                        </div>
                    </div> 
                    <div class="">
                        <img src="~\imagesurl-@item.GenerateName()" class="img-responsive" alt="...">
                    </div>
                    <div class="">
                        <p class="overlay__desc">
                           @item.Bio
                        </p>
                        <ul class="overlay__social">
                            <li><a href="#"><i class="ion-social-twitter"></i></a></li>
                            <li><a href="#"><i class="ion-social-facebook"></i></a></li>
                            <li><a href="#"><i class="ion-social-skype"></i></a></li>
                            <li><a href="#"><i class="ion-social-whatsapp-outline"></i></a></li>
                        </ul>
                        <a href="@Url.Action("Profile","Technician",new { url = item.GenerateURL() })" class="btn btn-accent">More info</a>


                    </div> 
                </div> 
            </div>
        }
        </div> <!-- / .row -->

Here is the View Model :-

 public class TechnicianViewModel
 {
 public Guid ID { get; set; }
 public string UserName { get; set; }
 public string Bio { get; set; }
 public string GenerateName()
 {
    string str = GenerateURL();
    str = str + ".jpg";
    return str;
 }
 public string GenerateURL()
 {
    string phrase = string.Format("{0}", UserName);
    string str = phrase.ToLower();
    str = Regex.Replace(str, @"\s", "-");
    return str;
 }
}

How can I avoid my controller method being an [HttpGet] as I had implemented here so that I can have the ID from the viewmodel object returned by the view.

        [HttpGet]
    [Route("technician/profile/calgary-{url}")]
    public IActionResult Profile(Guid? ID,string url)
    {
        var Profile = unitOfWork.TechnicianRepository.GetByGuidId(ID);
        return View(Profile);
    }

回答1:

In your View, change the link to the following:

<a href="@Url.Action("Profile","Technician",new { id = item.ID.ToString() })" class="btn btn-accent">More info</a>

You can get rid of the HttpGet and Route decorators on your controller method. Those will be taken care of by the default routing config:

public ActionResult Profile(string id)
{
    TempData["guid"] = id;
    return RedirectToAction("Profile");
}

public ActionResult Profile()
{
    Guid guid = Guid.Parse(TempData["guid"]);
    var Profile = unitOfWork.TechnicianRepository.GetByGuidId(guid);
    return View(Profile);
}