2
votes

I have a view bind with two models ...so I choose ViewModel method to bind two models with a view ...but I am facing problem to create a view of Create type ...but I am facing error ...FeedbackMix is ViewModel here...I have to pass query object for displaying something in the layout at the same time I have to make Create type view page ...
ERROR:

CS1061: 'FeedbackMixModel' does not contain a definition for 'Message' and no extension method 'Message' accepting a first argument of type 'FeedbackMixModel' could be found (are you missing a using directive or an assembly reference?)

Controller

public ActionResult Create()
{
   var msg= db.Messages.ToList();
    var feed = db.Feedbacks.ToList();
    FeedbackMixModel vm = new FeedbackMixModel();
    vm.allfeedbacks = feed;
   //this is also create type view 
    return View(vm);
}

View

@model WebApplication5.Models.FeedbackMixModel
....
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    ....
    @Html.LabelFor(model => model.Message, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.Message, new { htmlAttributes = new { @class = "form-control" } })
       @Html.ValidationMessageFor(model => model.Message, "", new { @class = "text-danger" })
    </div>
    ....
}

ViewModel

public class FeedbackMixModel
{
    public List<UserManager> allUserManagers { get; set; }
    public List<Feedback> allfeedbacks { get; set; }
    public List<Package> allpackages { get; set; }
    public List<Messages> allmessages { get; set; }
}

ERROR LINE

Line 16: @Html.LabelFor(model => model.Message, htmlAttributes: new { @class = "control-label col-md-2" })

1
The error message is pretty straight forward...in your FeedbackMixModel you don't have a Message property or methodHackerman
yep i know i pasted this code so that you people can understand easily ,,,i explain what exactly i want..you people can please paste your code how can i create view of type Create from this scenario.sajiii

1 Answers

1
votes

You don't have a property on your FeedbackViewModel called Message, which your LabelFor() helper is expecting so it doesn't know how to bind this :

public  class FeedbackMixModel
{
    public List<UserManager> allUserManagers { get; set; }
    public List<Feedback> allfeedbacks { get; set; }
    public List<Package> allpackages { get; set; }
    public List<Messages> allmessages { get; set; }

    // No property named Message here
}

If you want to have something like this, you'll need to add the property and populate it prior to passing it to the Model.

Are you sure that you aren't meaning to have a loop to iterate through your allMessages property and access this from those individual elements?

<h4>Messages</h4>
<hr />
@foreach(var message in Model.allMessages)
{
    <div class="form-group">
       @Html.LabelFor(message => message.Message, htmlAttributes: new { @class = "control-label col-md-2" })
       <div class="col-md-10">
             @Html.EditorFor(message => message.Message, new { htmlAttributes = new { @class = "form-control" } })
             @Html.ValidationMessageFor(message=> message.Message, "", new { @class = "text-danger" })
        </div>
    </div>
}