Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 4 在MVC 4.0中从MVC Viewmodel排除属性_Asp.net Mvc 4 - Fatal编程技术网

Asp.net mvc 4 在MVC 4.0中从MVC Viewmodel排除属性

Asp.net mvc 4 在MVC 4.0中从MVC Viewmodel排除属性,asp.net-mvc-4,Asp.net Mvc 4,我有自己的课程 public class Restaurant { [Key] public int RestId { get; set; } public string RestName { get; set; } public string Address { get; set; } public string City { get; set; } public int Pincode { get; set; } pub

我有自己的课程

public class Restaurant
{
    [Key]        
    public int RestId { get; set; }
    public string RestName { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public int Pincode { get; set; }
    public string Country { get; set; }
    public virtual ICollection<RestaurantReviews>Reviews { get; set; }


}

public class RestaurantReviews 
{
    [Key]
    public int ReviewId { get; set; }
    public int RestId { get; set; }
    [Required]
    public int Rating { get; set; }
    [Required]
    public string Review { get; set; }        
    public DateTime? CreatedOn { get; set; }          


}
我使用此ViewModel作为更新记录

 [HttpPost]
    public ActionResult EditReview(ViewRestReviews model)
    {

       // TryUpdateModel(model, excludedpro: new[] { "FirstName" });
        ModelState.Remove("Rating");
        TryUpdateModel(model);

        if (ModelState.IsValid)
        {                               
            ObjEntity.Entry(model.restaurantreviews).State = EntityState.Modified;
            ObjEntity.SaveChanges();
            return RedirectToAction("Index","Review", new { RestId = model.restaurantreviews.RestId });

        }
        return View(model);
    }

现在我的问题是,我不希望最终用户可以编辑“评级”。为此,我在类([Bind(EXclude=“Rating”)])甚至actionMethod上使用了EXclude属性。我使用排除。但对我来说什么都不管用。。。请帮助我了解如何从VIEWMODEL中排除属性。提前感谢

理想情况下,您应该拥有视图特定的模型(ViewModel),而不是在不同视图之间共享模型


因此,为编辑审阅操作创建一个单独的模型,该模型不具有“评级”属性。

您可以重新绑定该模型,因此我认为它将适用于您 如果你需要帮助,请告诉我


谢谢

谢谢您的回复,在ViewModel中有什么方法可以做到这一点吗?或者任何更好的方法……有单独的模型是标准实践。有一些选项,在绑定模型后,您必须显式地将其从模型中删除。但这些都不是好的选择。
 [HttpPost]
    public ActionResult EditReview(ViewRestReviews model)
    {

       // TryUpdateModel(model, excludedpro: new[] { "FirstName" });
        ModelState.Remove("Rating");
        TryUpdateModel(model);

        if (ModelState.IsValid)
        {                               
            ObjEntity.Entry(model.restaurantreviews).State = EntityState.Modified;
            ObjEntity.SaveChanges();
            return RedirectToAction("Index","Review", new { RestId = model.restaurantreviews.RestId });

        }
        return View(model);
    }