c# - Passing part of a viewmodel to a controller -
i have customer index page takes customerindexviewmodel populate page list of customers, time request took, , many other pieces of information.
i have customersearchargsmodel inside customerindexviewmodel.
public class customerindexviewmodel : baseindexviewmodel { public ienumerable<customer> customers{ get; set; } public double requesttime { get; set; } public customersearchargsmodel customersearchargsmodel { get; set; } public othertypes othertype {get;set;} } public class customersearchargsmodel { public string customerid { get; set; } public string firstname { get; set; } public string lastname { get; set; } }
on customer index page want have -
@model customerindexviewmodel @{ viewbag.title = "index"; } <h2>index</h2> @using (html.beginform("index","customer",formmethod.post, new { id="searchsubmit"})) { @html.labelfor(model => model.customersearchargsmodel.consumerid) @html.editorfor(model => model.customersearchargsmodel.consumerid) @html.labelfor(model => model.customersearchargsmodel.lastname) @html.editorfor(model => model.customersearchargsmodel.lastname) @html.labelfor(model => model.customersearchargsmodel.firstname) @html.editorfor(model => model.customersearchargsmodel.firstname) <input type="submit" value="search" /> }
i want return typed in values index (post) method on customer controller in customersearchargsmodel.
but don't know how return model different 1 defined @ top of page.
you can put "searchsubmit" form inside partial view. pass model.customersearchargsmodel partial view. sure model.customersearchargsmodel not null; otherwise, exception.
index page
@html.partial("_search", model.customersearchargsmodel)
_search partial view
@model customersearchargsmodel @using (html.beginform("index","customer",formmethod.post, new { id="searchsubmit"})) { @html.labelfor(model => model.consumerid) @html.editorfor(model => model.consumerid) @html.labelfor(model => model.lastname) @html.editorfor(model => model.lastname) @html.labelfor(model => model.firstname) @html.editorfor(model => model.firstname) <input type="submit" value="search" /> }
the problem approach 0 value displayed in textbox consumerid. solve problem can use html.action instead of html.partial.
hope helps.
Comments
Post a Comment