0

I have view to display Restaurant. In this view at the bottom of the page, I want to display Comments form to add comments about that Restaurant.

Can someone please help me to do this using MVC 4 & C#.

My Models has the followign two tables:

/Classifieds TABLE
public class Classifieds
{
    [Key]
    public string C_Unique_Id { get; set; }

    public string AdType { get; set; }

    public string Title { get; set; }

    public string Description { get; set; }

}

//ClassifiedsComments TABLE
public class ClassifiedsComments
    {
        [Key]
        public string CCommentsUniqueId { get; set; }

        public string CommentAuthor { get; set; }

        public string Comment { get; set; }

        [ForeignKey("Classifieds")]
        public string C_Unique_Id { get; set; }   //this is the foreign key of Classified record
        public virtual Classifieds Classifieds { get; set; }
    }

Classifieds Details view:

 @model SomeIndianShit.Models.Classifieds

@{
    ViewBag.Title = "Details";
}

                <table class="recordDetailsDisplayTableStype">
                    <tr>
                        <td colspan="2" align="left">
                            @Html.DisplayFor(model => model.Description) 
                            <br /><br />
                        </td>
                    </tr>

                    <tr>
                        <td>
                            Ad Type
                        </td>
                        <td align="left"> :
                            @Html.DisplayFor(model => model.AdType)
                        </td>
                    </tr>

                    SOME OTHER FIELDS DISPLAY HERE
                </table>


//Here I want to display "ClassifiedsComments" form to add comments to above Classified.
//HOW can I display the ClassifiedsComments create.cshtml code here??
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
Reddy
  • 97
  • 1
  • 3
  • 10

3 Answers3

1

Try this:

With View:

@using (Html.BeginForm("Classifieds", "ClassifiedsDetails", FormMethod.Post))

and

@using (Html.BeginForm("ClassifiedsComments", "ClassifiedsDetails", FormMethod.Post))

And use 1 Model for this:

public class ClassifiedsDetails
{
    public Classifieds Model1{ get; set; }

    public ClassifiedsComments Model2{ get; set; }
}

Update:

public class ClassifiedsDetails
{
    public ClassifiedsDetails()
    {
      Model1 = new Classifieds();
      Model2 = new ClassifiedsComments();
    }
    public Classifieds Model1{ get; set; }   
    public ClassifiedsComments Model2{ get; set; }
}

public class Classifieds
{
   public Classifieds()
   {
     C_Unique_Id = String.Emty;
     AdType = String.Emty;
     //---- Add default setting here------
   }
    [Key]
    public string C_Unique_Id { get; set; }

    public string AdType { get; set; }

    public string Title { get; set; }

    public string Description { get; set; }
}

Or display list of comments in View with Model:

public class ClassifiedsDetails
    {
        public ClassifiedsDetails()
        {
          Model1 = new Classifieds();
          Model2 = new List<ClassifiedsComments>();
        }
        public Classifieds Model1{ get; set; }   
        public List<ClassifiedsComments> Model2{ get; set; }
    }

View:

@model ClassifiedsDetails

@Html.LabelFor(model => model.Model1.Title)

@foreach (var items in Model.Model2)
{
   @item. //fields
}
Hau Le
  • 667
  • 2
  • 17
  • 42
  • I added same as above but in my Index controller object is coming as NULL. GET: /Classifieds/Details/5 public ActionResult Details(string id = null) { ClassifiedsDetails classifiedsdetails = db.ClassifiedsDetails.Find(id); – Reddy Feb 21 '14 at 04:41
  • In the above comment, classifiedsdetails is coming as NULL – Reddy Feb 21 '14 at 04:41
  • 1
    I think when use Model, you need declare a constructor, it will avoid a NULL exception or use ModelBinder with my answer: http://stackoverflow.com/questions/16046178/how-to-deal-with-many-possible-values-to-make-a-query/16053763#16053763 – Hau Le Feb 24 '14 at 10:03
  • Or use Ajax Form http://stackoverflow.com/questions/5410055/using-ajax-beginform-with-asp-net-mvc-3-razor – Hau Le Feb 24 '14 at 10:10
0

You can then add a ViewModel like this

public class ClassifiedsDetailViewModel
{
  public ClassifiedsDetailViewModel()
  {
      ClassifiedsComments = new ClassifiedsComments();
  }

  public Classifieds Classifieds { get; set; }
  public ClassifiedsComments ClassifiedsComments { get; set; }
}

Pass this view model to your view and then add @Html.Partial("_CreateClassifiedsCommentsFormPartial", Model.ClassifiedComments) below the table. And in your partial view, you can use Html.BeginForm or the Ajax.BeginForm. See sample here

drunkcoder
  • 311
  • 1
  • 6
  • An error message is displayed as "The model item passed into the dictionary is of type 'xyz.Models.Classifieds', but this dictionary requires a model item of type 'xyz.Models.ClassifiedsComments'." Here is what I did: -Added @Html.Partial("_CreateClassifiedsCommentsFormPartial") in Classifieds view – Reddy Feb 21 '14 at 03:38
  • Add this in your Classifieds class... public ICollection ClassifiedsComments { get; set; } then replace @Html.Partial("_CreateClassifiedsCommentsFormPartial") with @Html.Partial("_CreateClassifiedsCommentsFormPartial", Model.ClassifiedsComments) – drunkcoder Feb 21 '14 at 05:02
  • Classifieds class means Classifieds Details Controller or Model class?? – Reddy Feb 21 '14 at 05:09
  • still getting an error message as "The model item passed into the dictionary is of type 'SomeIndianShit.Models.Classifieds', but this dictionary requires a model item of type 'SomeIndianShit.Models.ClassifiedsComments'." at @Html.Partial("_CreateClassifiedsCommentsFormPartial", Model.ClassifiedsComments) – Reddy Feb 21 '14 at 05:16
  • or should I need a IEnumerable instead of ICollection – Reddy Feb 21 '14 at 05:18
  • Make sure that in your _CreateClassifiedsCommentsFormPartial, you have the correct model.. @model ClassifiedsComments – drunkcoder Feb 21 '14 at 05:32
0

To display data in view create view model, but to post comment, dont use model:

public class ClassifiedsViewModel
{
  public ClassifiedsViewModel()
  {
     Comments = new List<ClassifiedsComments>();
  }

  public Classifieds Classifieds { get; set; }
  public List<ClassifiedsComments> Comments { get; set; }
}

Fill this view model, use in view to display details and comments like above you write.

if(Model != null && Model.Classifieds != null)
{
  <table> ...display details.. </table>
}

if(Model != null && Model.Comments != null)
{
  <table> ...display comments with foreach loop.. </table>
}

And at bottom, create comment post form

@using (Html.BeginForm("SaveComment", "Controller", FormMethod.Post,                                   new { enctype = "multipart/form-data" }))
{

    //set to which Classifieds will this comment posted
    @Html.Hidden("C_Unique_Id", Model.Classifieds.C_Unique_Id)

    <fieldset>
        @Html.TextBox("Comment")

       //other fields ....

       <input type="submit" value="Save" />
    </fieldset>
}

Edit2: View model creating:

public ActionResult Details(int id)
{ 
    //add breakpoint here and follow any step.

    ClassifiedsViewModel viewModel = new ClassifiedsViewModel();
    viewModel.Classifieds = db.Classifieds.Find(id);
    viewModel.Comments = db.LoadCommentsByClassifiedsId(id); //create db method
    // or instead of this line use:
    viewModel.Comments = db.ClassifiedsComments.Where(e => e.C_Unique_Id == id).ToList();

    return(viewModel);
}
Jeyhun Rahimov
  • 3,769
  • 6
  • 47
  • 90
  • the view is fine but error message is displayed for Get Details method in controller. DO I HV TO USE Classifieds classifieds = db.Classifieds.Find(id); or ClassifiedsViewModel classifiedsViewModel = db.ClassifiedsViewModel.Find(id); – Reddy Feb 23 '14 at 01:45
  • If i use ClassifiedsViewModel classifiedsViewModel = db.ClassifiedsViewModel.Find(id); and find elements in Details view as Model.Classifieds.C_Unique_Id === blank page is displayed – Reddy Feb 23 '14 at 01:46
  • If i use Classifieds classifieds = db.Classifieds.Find(id); in Details method in controller, error message displayed as "The model item passed into the dictionary is of type 'SomeIndianShit.Models.Classifieds', but this dictionary requires a model item of type 'SomeIndianShit.Models.ClassifiedsViewModel'." – Reddy Feb 23 '14 at 01:48
  • I edited my answer and i advise you to define unique id as "int" not "string" – Jeyhun Rahimov Feb 23 '14 at 07:55
  • Hi Jhoon, Thank you very much for your help. Able to display the Classifieds. I cant define the unique as INT as It is breaking my whole solution. Can you please let me know how can I fix the viewModel.ClassifiedsComments = db.ClassifiedsComments.Where(e => e.C_Unique_Id == id); as I am gettign error message here saying " Cannot implicitly convert type 'System.Linq.IQueryable' to 'SomeIndianShit.Models.ClassifiedsComments'. An explicit conversion exists (are you missing a cast)?" – Reddy Feb 23 '14 at 23:34
  • it is casting problem, Oh, I have a misatake in view model, comments must be list; I edit view model, just now. Then after searching add '.ToList()' end of line – Jeyhun Rahimov Feb 24 '14 at 06:03
  • Hi Jhoon, with this new code, I am not able to find the elements to display in Details.cshtml view (@Html.DisplayFor(model => model.ClassifiedsComments.Comment)) - giving an error message here saying "....does not contain a definition for Comment and no extension method...." – Reddy Feb 24 '14 at 23:55
  • I think the problem is with @model SomeIndianShit.Models.ClassifiedsViewModel in Details.cshtml view. I need to add List here but it does not work for Classifieds.. – Reddy Feb 25 '14 at 03:16
  • You will not add view model list. will add only view model. it contains model self and comments list – Jeyhun Rahimov Feb 25 '14 at 06:11
  • Sorry so what is the issue here.. I did not understand it yet. – Reddy Feb 25 '14 at 22:33
  • Hi Jhoon, I really appreciate your help. can you please help me to fix it – Reddy Feb 25 '14 at 22:34
  • What error do you get? in what line, file? I edited answer, add a breakpoint and lets see where is problem – Jeyhun Rahimov Feb 26 '14 at 06:39
  • Hi Jhoon, If i declare int id, my whole solution is breaking. If declare as string id, and follow as above code, in View: Details.cshtml model => model.Comments.Comment is not available. Comments object is does not contain any values in it. Although Comment object have all the values in Controller when I debug with breakpoint – Reddy Feb 27 '14 at 04:00