如何获取视图以通过ViewModel将ID返回给控制器? [英] How to get the View to return the ID via a ViewModel to the controller?

查看:57
本文介绍了如何获取视图以通过ViewModel将ID返回给控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个视图,该视图通过TechnicianViewModel返回一个IEnumerable技术员集合.视图模型可以很好地填充并正确显示对象.

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.

如果我要单击技术人员上的More Info,则会启动[HttpGet]请求,这将导致出现一个queryString.这恰恰与该项目的要求背道而驰.(该项目的要求:URL中不应出现查询字符串)

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)

这是视图:-

  @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 -->

这是视图模型:-

 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;
 }
}

如何避免我在此处实现的控制器方法是[HttpGet],以便可以从视图返回的viewmodel对象中获得ID.

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);
    }

推荐答案

在您的视图中,将链接更改为以下内容:

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>

您可以在控制器方法上摆脱HttpGet和Route装饰器.这些将由默认的路由配置处理:

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);
}

这篇关于如何获取视图以通过ViewModel将ID返回给控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆