I want to access image from database direct in razor view.
This is my code
#model IEnumerable<TelerikMvcAppCombo.Models.ImageModel>
#{
Layout = null;
}
#foreach (var image in Model)
{
<img src=#image.IMAGESIZE_DESC/>
}
This is my model Class:-
[Table("IMAGESIZE")]
public class ImageModel
{
[Key]
public int IMAGESIZE_ID { get; set; }
public string IMAGESIZE_NAME { get; set; }
public string IMAGESIZE_DESC { get; set; }
public int created_by { get; set; }
public DateTime created_date { get; set; }
public int modified_by { get; set; }
public DateTime modified_date { get; set; }
}
This is my Controller Class:-
public JsonResult GetData([DataSourceRequest] DataSourceRequest request)
{
var list = db.imageModels.ToList();
return Json(list.ToDataSourceResult(request));
}
I don't know what's your problem exactly but try this:
#foreach (var image in Model)
{
<img src="#Url.Content(image.IMAGESIZE_DESC)" />
}
I don't know your view name but I assume that It is GetData.
You have to return ViewResult instead of JsonResult. You are returning JsonResult so it is not working.
public ActionResult GetData([DataSourceRequest] DataSourceRequest request)
{
var list = db.imageModels.ToList();
return View(list.ToDataSourceResult(request));
}
If you want to use JsonResult then you have to get data client side and bind them client side and in that case Razor view is not working.
Related
This is probably very simple for most MVC programmers, so maybe you can help me out. I have a table called Images in the database with nine columns. On my UPDATE, I just have three I want to mess with (ImageName, ImagePath, CourseID). After I post back with UPDATE, it sets the other six columns to NULL. I'm not sure how to handle this in MVC.:
My ViewModel:
public class GalleryViewModel
{
public Image _image { get; set; }
}
My Model:
public partial class Image
{
public int ImageID { get; set; }
public string ImageName { get; set; }
public string ImagePath { get; set; }
public Nullable<int> CourseID { get; set; }
public string UserId { get; set; }
public Nullable<System.DateTime> CreateDate { get; set; }
public string ImageType { get; set; }
public string ImageSize { get; set; }
public string FriendlyName { get; set; }
public virtual Cours Cours { get; set; }
}
My Controller:
[HttpGet]
public ActionResult UploadImageEdit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var model = new GalleryViewModel
{
_image = db.Images.Find(id),
};
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult UploadImageEdit(GalleryViewModel galleryViewModel)
{
if (ModelState.IsValid)
{
db.Entry(galleryViewModel._image).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("UploadImageIndex");
}
return View(galleryViewModel);
}
After reading other examples and becoming more familiar with ViewModel to controller, I got the UPDATE to work by doing the following:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult UploadImageEdit(int id, GalleryViewModel galleryViewModel)
{
if (ModelState.IsValid)
{
var imageContext = db.Images.Find(id);
imageContext.FriendlyName = galleryViewModel._image.FriendlyName;
db.Entry(imageContext).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("UploadImageIndex");
}
return View(galleryViewModel);
}
I hope this helps other folks out!
I am trying to display a grid in the MVC 3,
I got the following error:
A data source must be bound before this operation can be performed.
this is my Model:
public class EmpModel
{
public string UserName { get; set; }
public string Password { get; set; }
public int EmpID { get; set; }
public string EName { get; set; }
public int Age { get; set; }
public string Gender { get; set; }
public string Qualification { get; set; }
public string Address { get; set; }
public string EmailID { get; set; }
public int DeptID { get; set; }
public string DeptName { get; set; }
public string DeptHead { get; set; }
public int Deptnumber { get; set; }
}
This is Controller:
TestMVCEntities testEmp = new TestMVCEntities();
EmpModel empmod = new EmpModel();
public ActionResult Index()
{
List<EmpModel> emp = new List<EmpModel>();
return View();
}
This is my View:
#model IEnumerable<EmpApplication.Models.EmpModel>
#{
ViewBag.Title = "Index";
WebGrid grid = new WebGrid();
}
<h2>List of Employee</h2>
#grid.GetHtml(columns: new [] {
grid.Column("EmpID"),
grid.Column("EName"),
grid.Column("EmailID"),
grid.Column("Qualification")
})
You must pass as parameter the source of your data. The WebGrid expects a IEnumerable<Object> or IEnumerable<dynamic> as source.
You must return the list to your view:
public ActionResult Index()
{
List<EmpModel> emp = new List<EmpModel>();
return View(emp);
}
Then pass it to WebGrid constructor:
#{
ViewBag.Title = "Index";
WebGrid grid = new WebGrid(Model);;
}
I'm sure it's works..
public ActionResult Index()
{
List<EmpModel> emp = new List<EmpModel>();
return View(emp.AsEnumerable());
}
You just change your retrun list as enumerable.
fist model get the list of questions.
but i am not able to access them while using #Html.HiddenFor() etc
these item are visible if i use #Html.Hidden() or anything without ....For method...
any idea how can i do this
here are my classes
public class QuestionModel
{
public int Id { get; set; }
public string QuestDes { get; set; }
public int Aspect { get; set; }
}
public class AnswerModel
{
public int Id { get; set; }
public string SelectedAns { get; set; }
public virtual QuestionModel Question { get; set; }
public virtual PersonModel Person { get; set; }
}
my controller code
public ActionResult GPage2()
{
var tview = new Tuple<List<QuestionModel>,AnswerModel>(getQuestions(),new AnswerModel());
return View(tview);
}
private List<QuestionModel> getQuestions()
{
var qList = (from q in dbcon.Questions
orderby q.Id
select q).ToList();
return qList;
}
in cshtml page
#model Tuple<List<QuestionModel>,AnswerModel>
<td> #Html.Label(Model.Item2.SelectedAns)</td>
#Html.LabelFor(.......................) not working
from what you have posted you need to use a view model that includes your 2 models
public class ViewModel{
public List<QuestionModel> Questions { get; set; }
public List<AnswerModel> Answers { get; set; }
}
then on your view
#model ViewModel
using this setup your for helpers should work. since it is a list putting them in a foreach would look something like this.
#foreach(var temp in Model.Questions){
#Html.LabelFor(x => temp.Aspect)
//etc
}
I want get some qualification about reloading model in mvc action. For example:
I have some class model:
public class PresentationItemModel()
{
public int Id { get; set; }
public string Name { get; set; }
public string Title { get; set; }
public string Type { get; set; }
public List<int> PresentationIdList { get; set; }
}
And some controller action:
public ActionResult PostAction(PresentationItemModel model)
{
...
if(model.PresentationIdList == null)
{
model.PresentationIdList = new List<int>();
}
model.PresentationIdList.Add(model.Id);
...
...
...
}
I can call PostAction method several times and I want to save model.PresentationIdList result with all id's. But every time my PresentationIdList reloading with all model. But it's standard behavior.
Can I resolve it?
All you need to do is return the model object from your PostAction:
public ActionResult PostAction(PresentationItemModel model)
{
...
if(model.PresentationIdList == null)
{
model.PresentationIdList = new List<int>();
}
model.PresentationIdList.Add(model.Id);
...
...
...
return new ActionResult(model);
}
i am developing an application in MVC3.
Basically it is an Question Forum where on Question page user will see all questions as link and a post comment box at the end.and when he clicks on question link he is moved to the answers page.now the answer page is using another model class and their i am not able to acess my answer class data
what i found was every Controller has a view and a model
but i want my view to access multiple models and i dont knw how to go about it..
I tried this:
public class MainClass
{
public Question_Page question{ get; set; }
public Answer_Page answer { get; set; }
}
public class Question_Page
{
public virtual int Id { get; set; }
public virtual string Question_name { get; set; }
public virtual DateTime Created_Date { get; set; }
public virtual DateTime Modified_Date { get; set; }
public virtual int Created_By { get; set; }
public virtual int Modified_By { get; set; }
public virtual char Deleted { get; set; }
public Question_Page()
{
this.Deleted='F';
}
}
public class Answer_Page
{
public virtual int Id { get; set; }
public virtual string Answer { get; set; }
public virtual DateTime Created_Date { get; set; }
public virtual DateTime Modified_Date { get; set; }
public virtual int Created_By { get; set; }
public virtual int Modified_By { get; set; }
public virtual char Deleted { get; set; }
public virtual Question_Page Question { get; set; }
public Answer_Page()
{
this.Deleted='F';
}
}
Now inside the view of Questions List where i display a list of questions:
#model IEnumerable<Core.Model.MainPage>
#{
ViewBag.Title = "Index";
}
<style type="text/css">
ul
{
list-style-type: none;
}
</style>
<h2>All Questions</h2>
<hr />
#using (Html.BeginForm("Index","Question",FormMethod.Post))
{
#Html.ValidationSummary(true)
<ul>
#foreach (var item in Model)
{
<li>#Html.ActionLink(item.Question_name, "answer", new { Qry = item.Id })</li>
}
</ul>
<label for="PostyourQuestion:">Post your Question:</label><br /><br />
#Html.TextArea("textID")
<br />
<input type="submit"/>
}
after doing this much i am gettin errorin line:
#foreach (var item in Model)
This is my controller:
public ActionResult Index()
{
return View(new StudentService().GetAllStudents());
}
[HttpPost]
public ActionResult Index(Question_Page question,string textID)
{
question.Question_name = textID;
question.Created_Date = DateTime.Now;
question.Modified_Date = DateTime.Now;
question.Created_By = 101;
question.Modified_By = 101;
new StudentService().SaveOrUpdateStudent(question);
return View(new StudentService().GetAllStudents());
}
StudentService is a class where i hv defined the methods GetAllStudents(),SaveOrUpdateStudent()
Please help me
Your answer is rather simple I think?
#foreach (var item in Model)
should be
#foreach (var item in Model.Question_Page)
Your model is MainPage?
can you post that model here as well?
assume that GetAllStudents() return IEnumerable<Student>
in your controller
return View(new StudentService().GetAllStudents());
the actual Model send to View is IEnumerable<Student>
if you need IEnumerable<Core.Model.MainPage> as Model just return proper objects from your controller
IEnumerable<Core.Model.MainPage> model = //... define function that return Enumerable MainPage
return View(model);
If You want to use the same model on different views
public ActionResult Index()
{
//I am assuming, You are returning IEnumerable<MainClass>
List<MainClass> mainClass=new StudentService().GetAllStudents();
if(mainClass==null)
mainClass=new List<MainClass>(); //Incase you are not checking null value return
return View(mainClass); //return a none null IEnumerable<MainClass> object
}
[HttpPost]
public ActionResult Index(Question_Page question,string textID)
{
question.Question_name = textID;
question.Created_Date = DateTime.Now;
question.Modified_Date = DateTime.Now;
question.Created_By = 101;
question.Modified_By = 101;
new StudentService().SaveOrUpdateStudent(question);
return View(new StudentService().GetAllStudents());
}
Once your first controller method works, you will need to work on binding your model, for the second post method to work.
try viewModel. A model that have references to your desired model. so if a view is bound to your view model, then all the models referenced in the ViewModel will also get bonded.