asp.net mvc - How do I show content and insert data form in one view in mvc -
i have contact form show contact info , users can send feedback. know must use viewmodel
, use viewmodel
in view
. got below error:
the model item passed dictionary of type 'system.collections.generic.list`1[models.contactinfo]', dictionary requires model item of type 'finalkaminet.viewmodel.contactviewmodel'.
models:
public class contactus { [key] public int contactid { get; set; } public string fullname { get; set; } public string companyname { get; set; } public string email { get; set; } public string website { get; set; } public string contactmessage { get; set; } } public class contactinfo { [key] public int contactinfoid { get; set; } public string contacttype { get; set; } public string contactvalue { get; set; } }
viewmodel:
namespace finalkaminet.viewmodel { public class contactviewmodel { public contactus contactus { get; set; } public ienumerable<contactinfo> contactinfo { get; set; } } }
and in view:
@model finalkaminet.viewmodel.contactviewmodel <address> @foreach (var item in model.contactinfo) { <strong>@html.displayfor(modelitem => item.contacttype): </strong> <a href="mailto:html.displayfor(modelitem => item.contactvalue)">@html.displayfor(modelitem => item.contactvalue)</a><br /> } </address> @using (html.beginform()) { @html.antiforgerytoken() <div class="form-horizontal"> @html.validationsummary(true, "", new { @class = "text-danger" }) <div class="row"> <div class="col-md-7"> <div class="form-group"> @html.labelfor(model => model.contactus.fullname, htmlattributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.editorfor(model => model.contactus.fullname, new { htmlattributes = new { @class = "form-control" } }) @html.validationmessagefor(model => model.contactus.fullname, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10 pull-left"> <input type="submit" value="ارسال" class="btn btn-default" /> </div> </div> </div> <div class="col-md-5"></div> </div> </div> }
what wrong?
update:
namespace finalkaminet.controllers { public class contactcontroller : controller { // get: contact private applicationdbcontext db = new applicationdbcontext(); public actionresult contact() { viewbag.message = "از طریق فرم زیر می توانید برایمان پیغام بگذارید."; return view(db.contactsinfo.tolist()); } [httppost] [validateantiforgerytoken] public actionresult contact([bind(include = "contactid,fullname,companyname,phone,email,website,contactmessage")] contactus contactus) { if (modelstate.isvalid) { db.contactus.add(contactus); db.savechanges(); tempdata["notice"] = "پیغام شما با موفقیت ثبت شد."; return redirecttoaction("success"); } return view(); } public actionresult success() { return view("contact"); } } }
Comments
Post a Comment