can i fill model's fields by tds without javascript - asp.net-mvc

is it possible i fill model's fields by content of table's tds. of course without using javascript .
i wanna pass a model to view and get content of tds .
some thing like text box : #Html.TextBoxFor(m => m.username)
is it possible i have some thing like this for tds? what can i put in place of tds?
<tr class="darckTr">
<td>code :</td>
<td id="tdPobox" colspan="3">12345</td>
<td>Email :</td>
<td id="tdEmail">example#yahoo.com</td>
</tr>

It sounds like what you want is a display template. Create a .cshtml view, for example "ContactDetails.cshtml":
#model ContactDetails
<tr class="darckTr">
<td>code :</td>
<td id="tdPobox" colspan="3">#Model.PoBox</td>
<td>Email :</td>
<td id="tdEmail">#Model.Email</td>
</tr>
If the filename of your partial view matches the type in your model, it will be used automatically. Otherwise, you have a couple of options. Either specify the partial on your model, e.g.:
public class MyWrappingClass
{
[UIHint("_ContactDetails")]
public ContactDetails Details { get; set; }
}
And then do this in your view:
#model MyWrappingClass
#Html.DisplayFor(m => m.Details)
Or, just specify the template directly in your view:
#model MyWrappingClass
#Html.DisplayFor(m => m.Details, "_ContactDetails")
Brad Wilson wrote a good blog post on templates, including how they are resolved, here:
http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-3-default-templates.html
Edit
If you want to persist these values back on POST, you need to have an input element containing the values. In your case, the best way to do this would be to use a hidden field, for example:
#Html.HiddenFor(m => m.Username)

Related

How to read DropDownList value with HTML Helper when using #Html.PagedListPager MVC

I have the following code in my view:
<table>
<tr>
<td>
Number of Records to Show:
</td>
<td>
#Html.DropDownListFor(m=>m.NumRecsToShow, Enumerable.Range(1, 100).Select(i => new SelectListItem {Text = i.ToString(), Value = i.ToString()}))
</td>
</tr>
<tr>
#Html.PagedListPager((IPagedList) Model.lp, page => Url.Action("List", new RouteValueDictionary()
{
{"Page",page},
{"Recs",?????}
}))
</tr>
</table>
What I need to do is read the new selected value and pass it back as a "Recs" parameter, so I can return the right number of Records. But I dont know how to read the selected value when the PagedListPager number is clicked on in the table. I would love to know of some #Html helper (Where the ???? is in the code above.) that can read the value when I need it, but I am starting to think that I might need to use JQuery. Of course the other route would be if I can use the DropdownList to change the model data then I can just pass back the model to the controller, but the dropdownlist doesnt seem to get read unless I do a form submit.
Any pointing me in the right direction is appreciated.

Accessing individual item from model in partial view

In MVC 5, i am looping through the Model item in my view.
#foreach (var item in Model.invoices)
During each loop i am calling a partial view
#{
Html.RenderPartial("~/Views/Invoice/CommonSegment.cshtml", item);
}
But i am not able to access model ('item') in CommonSegment.cshtml
Snap from Visual Studio
<tr>
<td class="orderTitle">#item.orderId.label</td>
<td class="orderValue">#item.orderId.value</td>
</tr>
I am referring to same model in View and PartialView.
I wouldn't like to go with ViewBag.
Thanks in advance.
May be you can do this during the Loop,
#foreach (var item in Model.invoices)
{
#Html.RenderPartial("~/Views/Invoice/CommonSegment.cshtml", item);
}
But why writing foreach loops when you can use editor templates.
#model IEnumerable<Invoices>
#Html.EditorForModel()
and then simply define the corresponding editor template (~/Views/Invoice/CommonSegment.cshtml) It will automatically rendered for each element of your model:
#model Invoices
<div>
#Html.EditorFor(x => x.abc)
</div>

asp.net mvc - pass partial data model to partial view

I wish to build a partial view that gets a model column and print it.
Something like that:
At the view:
#model IEnumerable<products_comparison.Models.Product>
#{
ViewBag.Title = "Index";
var Brand = (from r in Model
select r.Brand).Distinct();
}
<h2>
Index</h2>
#Html.RenderPartial("_DisplayAttribute",Brand)
And at the partial view:
<table>
<tr>
<th>
Brand
</th>
</tr>
#foreach (var row in Model)
{
<tr>
<td>
#Html.DisplayFor(r => row)
</td>
</tr>
}
</table>
There are a few problems I run into:
The compiler doesnt allow me to send Barnd to the partial view.
If you look at the partial view code you will see the word Brand, which is the column name. I dont wish to hard-coded the word "Brand" in the partial view, instead I like that the column name will be there.
In the partial view I need to put #model products_comparison.Models.Product, but I dont
want to send the hole table. I want to send only one column - But I dont know what to put there..
Thanks!
EDIT:
Just to clear one thing, I want that the view will call the same partial view for each column in the table(for most of the columns in the table anyway) and each time I'll send a different column(distinct value column to be exact).
Start by refactoring and putting the right logic into the right place. This LINQ query has strictly nothing to do in a view. A view is not supposed to do any LINQ queries or whatever to pull data. A view is supposed to work with data that it is passed to it from the controller action under the form of a view model. A controller action builds and passes an adapted view model that you define for the view.
So as always you start by defining a view model that will be adapted to the requirements of your view:
public class MyViewModel
{
public IEnumerable<Brand> Brands { get; set; }
}
then you write a controller action that will populate this view model and pass it to the view:
public ActionResult Foo()
{
IEnumerable<products_comparison.Models.Product> products = ...
var model = new MyViewModel
{
Brands = (from r in Model select r.Brand).Distinct()
};
return View(model);
}
then a view:
#model MyViewModel
<table>
<tr>
<th>
Brand
</th>
</tr>
#Html.DisplayFor(x => x.Brands)
</table>
and finally you could define a corresponding display template which will automatically be rendered for each element of the Brands collection of your view model (~/Views/Shared/DisplayTemplates/Brand.cshtml):
#model Brand
<tr>
<td>
#Html.DisplayForModel()
</td>
</tr>
For 1 try changing #Html.RenderPartial("_DisplayAttribute",Brand) to #Html.Partial("_DisplayAttribute",Brand)
You will also need to specify the model in the partial view like #model products_comparison.Models.Brand or something like it
Also please clarify 2 & 3 as they are not clear what you want

How to get an html table back to a Model?

I'll use the famous NerdDinner as an example here.
I have a search page where the user can enter a search string and then see the result in a table below. The user can also add more results to the table, like this:
Search for dinners today and display in a table.
Search for dinners tomorrow and add the result to the table.
The table will now show dinners today and tomorrow.
The user is also able to remove dinners from the table by clicking on them, one by one.
I need to generate a pdf with the results in the table. Not like a print screen because the pdf has it's own layout. I just need the data in the table. Preferably in a list of Dinner models.
Right now I can generate a pdf from a list of Dinner models. But once I've printed them to the table and the user has manipulated it I don't know how to get it back to a list of Dinner models.
Another solution could be to keep the Id's hidden in the table and then do another search in the DB with the Id's from the table (after the user has manipulated it). At least then I would get the result in the form of a list of Dinners. But this seems redundant to me.
Has anyone had a similar problem and how did you solve it?
You could put the table inside an html <form> and on each row in addition to the label you could have a hidden field:
#model IEnumerable<Dinner>
#using (Html.BeginForm())
{
<table>
<thead>
<tr>
<th>Prop1</th>
<th>Prop2</th>
<th>Prop3</th>
</tr>
</thead>
<tbody>
#Html.EditorForModel()
</tbody>
</table>
<input type="submit" value="Export to PDF" />
}
and in the editor template:
#model Dinner
<tr>
<td>
#Html.DisplayFor(x => x.Prop1)
#Html.HiddenFor(x => x.Prop1)
</td>
<td>
#Html.DisplayFor(x => x.Prop2)
#Html.HiddenFor(x => x.Prop2)
</td>
<td>
#Html.DisplayFor(x => x.Prop3)
#Html.HiddenFor(x => x.Prop3)
</td>
</tr>
Now this form could be submitted to the following controller action:
public ActionResult GeneratePdf(IEnumerable<Dinner> dinners)
{
byte[] pdf = ...
return File(pdf, "application/pdf", "dinners.pdf");
}
You may also checkout the following blog post for managing a variable length list in order to keep input field names in sync for the binder when adding/removing elements.

MVC: I want the Date in my View not to show the default value

I am displaying a Date in my View. This particular View is one that I use to add data to the database.
On the database the Date field is not null, and that is how it should be. However by virtue of it being not null, the same date field is mapped as not null in the EF generated classes I use. So when I send an empty object of that class to the View, the date is given the default minimum value, which I do not want. Instead I want to see a blank field.
How do I do this?
The date in the View look as follows (not sure this helps anyone, but I know I always get asked for the code)
<tr>
<td>
<%: Html.LabelFor(model => model.OtherLeaveDate)%>
</td>
<td>
<%: Html.EditorFor(model => model.OtherLeaveDate)%>
<%: Html.ValidationMessageFor(model => model.OtherLeaveDate)%>
</td>
</tr>
<tr>
I'm afraid I don't know the specifics of how this works with EF or MVC, but have you tried making the field a Nullable<DateTime> instead of just a DateTime? (Which can also be written in the format DateTime? mydate). This allows the field to contain nothing.
Have you checked out the DefaultValue attribute, here is a link for this.

Resources