asp.net mvc - Add ID from URL in MVC -


im starting out mvc , i'm in learning phase. i'm stuck problem. in soulution got these 2 tables.

topictable -topid -topname

contenttable -contid -topid -content

i want while i'm in topic detailes-view able create new content.
in detailes.cshtml file i've added:

@html.actionlink("create new content", "create", "content", new { id = model.topicid}, null)` display in url: localhost/content/create/1 

in contentcontroller ive got this, , error when submitting "validation failed 1 or more entities. see 'entityvalidationerrors' property more details."

        [httppost]         [validateantiforgerytoken]         public actionresult create(int id)         {             contentmodel content = new contentmodel();             if (modelstate.isvalid)             {                 content.topid = id;                 db.contentmodel.add(content);                 db.savechanges();                 return redirecttoaction("index");              }             return view(content)         } 

the view:

@model webapplication2.models.contentmodel  @{     viewbag.title = "create"; }  @using (html.beginform())  {     @html.antiforgerytoken()      <div class="form-horizontal">         <h4>joboffermodel</h4>         <hr />         @html.validationsummary(true)  <div class="form-group">             @html.labelfor(model => model.content, new { @class = "control-label col-md-2" })             <div class="col-md-10">                 @html.editorfor(model => model.content)                 @html.validationmessagefor(model => model.content)             </div>         </div>       <div class="form-group">         <div class="col-md-offset-2 col-md-10">             <input type="submit" value="create" class="btn btn-default" />         </div>     </div> </div> }  <div>     @html.actionlink("back list", "index") </div>  @section scripts {     @scripts.render("~/bundles/jqueryval") } 

model:

public class topic { [display(name = "id")]         public virtual int topicid{ get; set; }         [display(name = "name")]         public virtual string topicname{ get; set; }   }   public class content { [display(name = "id")]         [display(name = "id")]     public virtual int contentid{ get; set; }     [display(name = "topicid")]     public virtual string topicid{ get; set; }     [allowhtml]     [display(name = "innihald")]     public virtual string content { get; set; }   } 

so when i'm creating new content topid value automaticly added content table.

would love help,, gracias

@html.actionlink() call method. need return view renders form, , post method receive , save form data

public actionresult create(int id) {   content model = new content();   model.topicid = id; // note topicid should int, not string   return view(model); }  [httppost] [validateantiforgerytoken] public actionresult create(content model) {   if(!modelstate.isvalid)   {     return view(model);   }   // save model , redirect. } 

view (divs etc omitted)

@model webapplication2.models.content .... @using (html.beginform())  {   @html.antiforgerytoken()   @html.validationsummary(true)   @html.hiddenfor(m => m.topicid) // postback   @html.labelfor(model => model.content, new { @class = "control-label col-md-2" })   @html.textboxfor(model => model.content)   @html.validationmessagefor(model => model.content)   <input type="submit" value="create" class="btn btn-default" /> } .... 

Comments

Popular posts from this blog

c++ - OpenMP unpredictable overhead -

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

javascript - Wordpress slider, not displayed 100% width -