Reputation: 32059
Here is my Complex ViewModel
public class OperationStudyInputViewModel
{
public OperationStudy OperationStudy { get; set; }
public FileUpload FileUploads { get; set; }
public IEnumerable<string> Attachments { get; set; }
public IEnumerable<string> Folders { get; set; }
}
Here is the OperationStudy Model
public class OperationStudy
{
public int OperationStudyId { get; set; }
public string SpCategoryId { get; set; }
//Here is some Other Properties
}
Here is the OperationStudyInput() Post Method
[HttpPost]
public ActionResult OperationStudyInput([Bind(Exclude = "SpCategoryId")] OperationStudyInputViewModel inputViewModel, IEnumerable<HttpPostedFileBase> multiplefiles)
{
// some Necessary codes Here
_dbContext.OperationStudies.Add(inputViewModel.OperationStudy);
_dbContext.SaveChanges();
}
I want to exclude SpCategoryId
From OperationStudy in inputViewModel(inputViewModel.OperationStudy) in OperationStudyInput() Post mehtod. I have tried with the previous code but its not working as expected!!
Any Help Please!!
Upvotes: 0
Views: 712
Reputation: 863
You can specify the Bind attribute over the class like this:
[Bind(Exclude = "SpCategoryId")]
public class OperationStudy
{
public int OperationStudyId { get; set; }
public string SpCategoryId { get; set; }
}
But, I would recommend to create and use separate ViewModel for binding.
Upvotes: 0
Reputation: 30175
I would recommend to distinguish between view models and data models that you have. If you return directly then it can lead to unexpected security troubles (suddenly adding a field in backend will lead to this field exposed to web). So what I would do is create a number of web models, that you can suffix with Web e.g. and do a mapping each time you need to return it.
This will provide a good separation between your view and BI layers. To help you achieve that you can use frameworks like AutoMapper
. It is not necessary, but helps you with crud mapping.
You might think this is an overkill, but trust me, you will see advantages of this approach in the long run. You can search more on google about the topic of separation as well.
P.S. I would not suggest any hacks with removing the data from model like suggested in other posts. This seems like a very fragile approach to me.
Upvotes: 1